1

I'm trying to trigger one JS file from Electron. If I try the command node test.js in the terminal, it's working fine. If I try the same thing in Electron, I'm getting an error 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.

nateyolles
  • 1,851
  • 5
  • 17
  • 23
neel
  • 779
  • 1
  • 6
  • 11

2 Answers2

0

Check out this answer containing several good methods to try and debug this error type.

Community
  • 1
  • 1
Jens Habegger
  • 5,266
  • 41
  • 57
0

Extremely late to the party, but node's fork exists exactly for the purpose of running an external node file.

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

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.

P.S.: The linked SO post in the first answer has more information on the ENOENT error concerning spawn.

Dan Balaban
  • 735
  • 8
  • 10