简体   繁体   中英

Ignore filename extension for child_process fork using node-dev and Typescript

I have a project written in Typescript, and I'm using node-dev alongside ts-node in my local enviroment for development. I'm using child_process 's fork method to instantiate a subprocess, like so:

fork(path.join(__dirname, './worker.ts'));

This works fine, and I can even set breakpoints in VS Code for the worker.

The problem is that when building (transpiling) my project, a MODULE_NOT_FOUND exception is thrown because worker.ts turned into worker.js . Initially, mi idea was to omit the file extension when forking ( fork(path.join(__dirname, './worker')); ), but if I do so, when running the project with node-dev , it throws a MODULE_NOT_FOUND because it can't resolve the file if the extension is not present.

Is there any workaround for this? Maybe an extra configuration option for node-dev ?

I'm on Windows 10 using node v12.22.1

A simple solution I suggest would be to read in the current file type. The compiled files should end with ".js" while the source files end with ".ts". Using the path.extname methode in combination with __filename you can easily extract that file extension and simply concat it to your file name.

fork(path.join(__dirname, './worker' + path.extname(__filename)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM