[英]How to trigger node filename.js file in Electron?
I'm trying to trigger one JS file from Electron. 我正试图从Electron触发一个JS文件。 If I try the command node test.js in the terminal, it's working fine.
如果我在终端中尝试命令节点test.js,它工作正常。 If I try the same thing in Electron, I'm getting an error
Uncaught Error: spawn node test.js ENOENT
. 如果我在Electron中尝试相同的操作,我会收到错误
Uncaught Error: spawn node test.js ENOENT
。 Can you correct me if I'm on the wrong path? 如果我走错了路,你能纠正我吗?
var spawn = require('child_process').spawn;
var executeSpawn = spawn('node test.js',{
cwd: process.resourcesPath+'/app/test.js'});
executeSpawn.stdout.on('data',function(data){
console.log(`data:${data}`);
});
executeSpawn.stderr.on('data',function(data){
console.log("data:",data);
});
executeSpawn.on('close',function(ev){
console.log("ev",ev);
});
Thanks in advance. 提前致谢。
查看此答案,其中包含几种尝试调试此错误类型的好方法。
Extremely late to the party, but node's fork
exists exactly for the purpose of running an external node file. 派对非常晚,但节点的
fork
完全存在于运行外部节点文件的目的。
parent.js parent.js
const { fork } = require('child_process');
const forked = fork('child.js');
forked.on('message', (msg) => {
console.log('Message from child', msg);
});
forked.send({ hello: 'world' });
child.js child.js
process.on('message', (msg) => {
console.log('Message from parent:', msg);
});
let counter = 0;
setInterval(() => {
process.send({ counter: counter++ });
}, 1000);
Example shamelessly taken from this freecodecamp tutorial on Node.js child processes . 无耻地从这个关于Node.js子进程的freecodecamp教程中获取的示例。
PS: The linked SO post in the first answer has more information on the ENOENT
error concerning spawn
. PS:第一个答案中的链接SO帖子有关于
spawn
的ENOENT
错误的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.