简体   繁体   中英

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. Also, electron ProjectInitializer__ElectronMain.js works, it to execute this console command from my project directory.

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.xx 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.xx knows, where should to find the electron. Unfortunately, execa has too small community and small documentations, so stopped to use it.

Additional information

How I run this Node.js script which has the spawn parameter

By command my-library init which I created.

In package.json:

"bin": {
  "my-library": "bin/my-library"
}

In bin/my-library (no filename extension):

#!/usr/bin/env node

require('../CLI').interpretConsoleCommandAndExecute(process.argv);

In CLI.js I parse the console command, and if it is the my-library init , I'll try to execute

const childProcess: ChildProcess__type = ChildProcess.spawn(
   'electron',
   [ 'ProjectInitializer__ElectronMain.js' ],
   { cwd: __dirname }
);

console.log(process.env) output

Below output is for PhpStorm build-in console, however in other consoles, eg in cmder, output is different.

{ FPS_BROWSER_APP_PROFILE_STRING: 'Internet Explorer',
  CommonProgramFiles: 'C:\\Program Files\\Common Files',
  PROMPT: '$P$G',
  SESSIONNAME: 'Console',
  COMPUTERNAME: 'MSI',
  OneDriveConsumer: 'D:\\OneDrive',
  __INTELLIJ_COMMAND_HISTFILE__:
   'C:\\Users\\i\\.PhpStorm2019.1\\config\\terminal\\history\\history-34',
  SystemDrive: 'C:',
  NUMBER_OF_PROCESSORS: '12',
  LOGONSERVER: '\\\\MSI',
  TEMP: 'C:\\Users\\i\\AppData\\Local\\Temp',
  TMP: 'C:\\Users\\i\\AppData\\Local\\Temp',
  HOMEPATH: '\\Users\\i',
  PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JSE;.WSF;.WSH;.MSC',
  USERNAME: 'i',
  ProgramFiles: 'C:\\Program Files',
  USERDOMAIN_ROAMINGPROFILE: 'MSI',
  LOCALAPPDATA: 'C:\\Users\\i\\AppData\\Local',
  TERMINAL_EMULATOR: 'JetBrains-JediTerm',
  PROCESSOR_IDENTIFIER: 'Intel64 Family 6 Model 158 Stepping 10, GenuineIntel',
  DriverData: 'C:\\Windows\\System32\\Drivers\\DriverData',
  APPDATA: 'C:\\Users\\i\\AppData\\Roaming',
  ALLUSERSPROFILE: 'C:\\ProgramData',
  USERDOMAIN: 'MSI',
  OS: 'Windows_NT',
  PROCESSOR_LEVEL: '6',
  ProgramData: 'C:\\ProgramData',
  ComSpec: 'C:\\Windows\\system32\\cmd.exe',
  PROCESSOR_ARCHITECTURE: 'AMD64',
  FPS_BROWSER_USER_PROFILE_STRING: 'Default',
  SystemRoot: 'C:\\Windows',
  PROCESSOR_REVISION: '9e0a',
  OneDrive: 'D:\\OneDrive',
  PSModulePath:
   'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules',
  PATH:
   'D:\\PhpStorm\\InHouseDevelopment\\my-library\\node_modules\\.bin;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Program Files\\Docker\\Docker\\Resources\\bin;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Program Files
\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Pro
gram Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\I
ntel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Common Files\\Acronis\\VirtualFile\\;C:\\Program Files (x86)\\Common Files\\Acronis\\VirtualFile64\\;C:\\Program Files (x86)\\Com
mon Files\\Acronis\\FileProtector\\;C:\\Program Files (x86)\\Common Files\\Acronis\\FileProtector64\\;C:\\Program Files (x86)\\Common Files\\Acronis\\SnapAPI\\;C:\\Program Files\\nodejs\\;C:\\Program Files\\Git\\cmd;C:\\Program Files (x86)\\Yarn\\bin\\;C:\\Users\\t
okug\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\i\\AppData\\Roaming\\npm;C:\\Users\\i\\AppData\\Local\\Yarn\\bin;C:\\texlive\\2019\\bin\\win32',
  'ProgramFiles(x86)': 'C:\\Program Files (x86)',
  USERPROFILE: 'C:\\Users\\i',
  windir: 'C:\\Windows',
  ProgramW6432: 'C:\\Program Files',
  configsetroot: 'C:\\Windows\\ConfigSetRoot',
  'CommonProgramFiles(x86)': 'C:\\Program Files (x86)\\Common Files',
  PUBLIC: 'C:\\Users\\Public',
  HOMEDRIVE: 'C:',
  CommonProgramW6432: 'C:\\Program Files\\Common Files' }

Trying to execute ChildProcess.spawn('env')

In Php Strorm console, it causes familiar Error: spawn env ENOENT .

As discussed in the chat, the error you are getting is usually caused by the fact that the executable you are trying to run is not available

Now there are multiple reasons the executable may not be available

  1. The executable is not there at all anywhere on the system
  2. The executable is there but not in the folders defined by system's PATH variable
  3. The executable is there in the current directory but the directory context in which the process is being run is different

To fix #1 and #2 you just install the executable globally in system PATH

For fixing #3 you can do two things. Add the path for the current directory ( { cwd: __dirname} ) and also a relative path to executable

const childProcess: ChildProcess__type = ChildProcess.spawn( 
Path.resolve(__dirname, 'node_modules/.bin/electron'), 
[ Path.resolve(__dirname, 'ProjectInitializer__ElectronMain.js') ], 
{ cwd: __dirname} 
); 

or

const childProcess: ChildProcess__type = ChildProcess.spawn( 
'./node_modules/.bin/electron'), 
[ Path.resolve(__dirname, 'ProjectInitializer__ElectronMain.js') ], 
{ cwd: __dirname} 
); 

or

const childProcess: ChildProcess__type = ChildProcess.spawn( 
'node_modules/.bin/electron', 
[ './ProjectInitializer__ElectronMain.js' ], 
{ cwd: __dirname} 
); 

In case you decide to override the PATH environment variable you can do it passing the env parameters with more values

const childProcess: ChildProcess__type = ChildProcess.spawn( 
'node_modules/.bin/electron', 
[ './ProjectInitializer__ElectronMain.js' ], 
{ cwd: __dirname, env: {....}} 
); 

You can use the existing environment variables from process.env and then update the same, and pass it to env parameter

it's likely that you need to specify the full path to the electron command, since it isn't on your path.

If you are running your script from your project root, electron is probably at in ./node_modules/.bin/electron , if they packaged up the app to be run that way.

I would guess that your alternative library checks node_modules by default.

The alternative would be to ensure electron is in your path, but that requires updating your system configuration, which it would be weird for a library to do.

Edit: example of call with path:

const childProcess: ChildProcess__type = ChildProcess.spawn(
   'node_modules/.bin/electron',
   [ 'ProjectInitializer__ElectronMain.js' ],
   { cwd: __dirname }
);

I'd also add some dumb logging on the process, so you know why the process failed:

function log(data) {
    console.log("" + data)
}
child_process.stdout.on('data', log)
child_process.stderr.on('data', log)
child_process.on('close', log)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM