Working with Node.js 23.8 and want to run ts-node as a child process. I'm using ts-node to run cli.ts from the lv_img_conv library. I've installed both the lv_img_conv and ts-node libraries globally. Also added the node_modules folder that holds global libraries to system environment variable PATH (using Windows 11, 64 bit). But the following code:
error: spawn ts-node ENOENT
child process exited with code -4058
How to resolve this so that ts-node can be run effectively in child_process.spawn()?
Working with Node.js 23.8 and want to run ts-node as a child process. I'm using ts-node to run cli.ts from the lv_img_conv library. I've installed both the lv_img_conv and ts-node libraries globally. Also added the node_modules folder that holds global libraries to system environment variable PATH (using Windows 11, 64 bit). But the following code:
error: spawn ts-node ENOENT
child process exited with code -4058
How to resolve this so that ts-node can be run effectively in child_process.spawn()?
node.js
child-process
spawn
ts-node
Share
Improve this question
edited Mar 31 at 9:02
jonrsharpe
122k3030 gold badges268268 silver badges475475 bronze badges
asked Mar 31 at 8:59
coder101coder10147355 silver badges3030 bronze badges7
Avoid using global packages for local purposes. If it's project dependency, it needs to be in devDependencies. You can call it as "npx ts-node". There's a chance it won't work this way because it needs to take tsconfig from lv_img_conv into account
– Estus Flask
CommentedMar 31 at 9:38
@jonrsharpe Tried it as a local package also, as well as with "npx ts-node". Same errors persist. What do you mean by, "it needs to take tsconfig form lv_img_conv into account"?
– coder101
CommentedMar 31 at 9:40
1"Tried it as a local package also, as well as with "npx ts-node" - you need both, this way it's supposed to be executed from node_modules/.bin/ts-node. Also use exec instead of spawn. TS needs to use tsconfig from the project in order to compile it correctly. I'd expect it to be something like npx ts-node --project .../lv_img_conv/tsconfig.json .../lv_img_conv/lib/cli.ts
– Estus Flask
CommentedMar 31 at 10:03
Added {cwd:'./../external_libraries/lv_img_conv-master'} after the arguments to spawn, which is where tsconfig.json for lv_img_conv-master resides. Now i get "spawn npx ts-node ENOENT" for an error. But why would cwd be required if I'm providing the absolute path for cli.ts, the first argument to ts-node?
– coder101
CommentedMar 31 at 10:04
1As said, use exec instead of spawn. You need to ts-node to be aware of the project it compiles, specifically its tsconfig. This may not be enough to use --project as in the comment above because you need the deps to match exactly the ones that this project uses in order to compile it successfully. . From your case it looks like you need to run "npm i" in lv_img_conv folder and then exec npx ts-node from that location, i.e. cwd: ".../lv_img_conv".
– Estus Flask
CommentedMar 31 at 10:08
As Estus Flask points out in comments, ts-node is a shell command, so the shell option to spawn must be included. What's also required is installation of @swc/core to be used as a transpiler by cli.ts. Installing ts-node in node_modules doesn't install @swc/core by default.
npx ts-node --project .../lv_img_conv/tsconfig.json .../lv_img_conv/lib/cli.ts
– Estus Flask Commented Mar 31 at 10:03npx ts-node
from that location, i.e.cwd: ".../lv_img_conv"
. – Estus Flask Commented Mar 31 at 10:08