简体   繁体   English

如何在Electron中触发节点filename.js文件?

[英]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帖子有关于spawnENOENT错误的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “node filename.js”在 vs 代码中不起作用 - “node filename.js” is not working in vs code 运行节点fileName.js命令后如何返回终端执行? - How to return to terminal execution after running node fileName.js command? 在Docker容器中运行命令&#39;node filename.js&#39; - Run command 'node filename.js' inside a Docker container 当我输入终端命令“$ node fileName.js”时,concole 会抛出“SyntaxError: Unexpected token &#39;&lt;&#39;”错误 - When I enter the terminal command "$ node fileName.js" the concole throws out a "SyntaxError: Unexpected token '<'" error jscs 错误:validateLineBreaks:filename.js 处的换行符无效 - jscs error : validateLineBreaks: Invalid line break at filename.js js 文件应该有什么名称,带有奇怪结尾的 url 引导:“.../filename.js?crc=6»? - What names should js files have, to which urls with a strange ending lead: ".../filename.js?crc=6»? ver = 3.1.2是什么意思?在ajax.js或filename.js末尾的ver = 3.1.2是什么意思? - What does ver=3.1.2 at the end of ajax.js or filename.js?ver=3.1.2 mean? 如何将值作为参数从节点 js 文件发送到电子 main.js 文件的 loadURL()? - how to send value as a parameter to loadURL() of electron main.js file from node js file? Node.js 下载文件问题 - 文件名 - Node.js problem with downloading file - filename 如何在电子renderer.js文件中使用Node.js解析JSON? - How can I parsing JSON with Node.js in electron renderer.js file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM