Wednesday, 26 June 2019

How to execute locally installed Node.js application by child_process.spawn()?

I want to execute the Electron application by child_process.spawn:

import ChildProcess, {ChildProcess as ChildProcess__type} from 'child_process';
const childProcess: ChildProcess__type = ChildProcess.spawn(
        'electron',
        ['ProjectInitializer__ElectronMain.js'],
        { cwd: __dirname } // current project root
    );

I got Error: spawn electron ENOENT error. Electron has been installed locally, AFAIK is the good practice.

Following frequently up-voted ENOENT error debugging guidance, I got the cause: there is no directory among process.env.PATH, which includes electron.

I know about PATH variable not much, so I can not answer what must be in this variable and what is not. But what I want to ask is: how to execute locally installed (in node_modules) Node.js applications (like electron)?

By the way, execa which is known as improved child_process runs electron without errors (update: version 2.x.x already do not runs):

import executeExternalCommand, { ExecaReturnValue } from 'execa';

try {
  await executeExternalCommand(
      'electron',
      ['ProjectInitializer__ElectronMain.js'],
      { cwd: __dirname }
  );
} catch (error) {
  console.error(error);
}

Somehow, thanks to { cwd: __dirname }, the execa v 1.x.x knows, where should to find the electron. Unfortunately, execa has too small community and small documentations, so stopped to use it.



from How to execute locally installed Node.js application by child_process.spawn()?

No comments:

Post a Comment