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

719 views Asked by At

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

1

There are 1 answers

3
CptPiepmatz On BEST ANSWER

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)));