[英]nodejs child process - Error: spawn node ENOENT
I'm using this code to try spawn a child process that needs to run a script.我正在使用此代码尝试生成一个需要运行脚本的子进程。
const child = require('child_process').spawn;
const path = require('path');
const script = path.format({dir: __dirname, base: 'myscript.js'});
child('node', [script], {
cwd: __dirname,
env: {
TEST_USERNAME: 'myuser',
TEST_PASSWORD: 'mypassword'
}
}).on('error', (error) => {
console.log(error);
});
I'm getting this error when I try to run my index.js
script尝试运行
index.js
脚本时出现此错误
Error: spawn node ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:276:19)
at onErrorNT (node:internal/child_process:476:16)
at processTicksAndRejections (node:internal/process/task_queues:80:21) {
errno: -2,
code: 'ENOENT',
syscall: 'spawn node',
path: 'node',
spawnargs: [ '/Users/dev/Desktop/cli-training/demo.js' ]
}
UPDATE更新
I'm trying to run the child script after that inquirer.js
have prompted some questions to the user.在
inquirer.js
向用户提示一些问题之后,我正在尝试运行子脚本。 Can be this the problem?I've noticed that the child process will be not executed.可能是这个问题吗?我注意到子进程不会被执行。 Inside my child script I have this code
在我的子脚本中,我有这个代码
const { IgApiClient } = require('instagram-private-api');
const chalk = require('chalk');
console.log(chalk.magenta('Starting child script'));
const ig = new IgApiClient();
console.log(process.argv);
How I can fix it?我该如何解决?
If you want to execute the node binary in a child-process, it s better to refer to its full path, you can find it with process.execPath
如果要在子进程中执行节点二进制文件,最好参考其完整路径,可以使用
process.execPath
找到它
this should fix your problem这应该可以解决您的问题
child(process.execPath, [script], {
cwd: __dirname,
env: {
TEST_USERNAME: 'myuser',
TEST_PASSWORD: 'mypassword'
}
}).on('error', (error) => {
console.log(error);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.