How do I debug “Error: spawn ENOENT” on node.js?

后端 未结 25 2629
轻奢々
轻奢々 2020-11-22 03:00

When I get the following error:

events.js:72
        throw er; // Unhandled \'error\' event
              ^
Error: spawn ENOENT
    at errnoException (chil         


        
相关标签:
25条回答
  • 2020-11-22 03:32

    How to research the spawn call raising the error:

    • Use NODE_DEBUG=child_process, Credits to @karl-richter. Simple, quick, October 2019
    • Use a wrapper to decorate child_process.spawn, Credits to @jiaji-zhou. Simple, quick, January 2015
    • Long procedure, credits to @laconbass. Complex, time-cost, December 2014

    Known, usual causes

    1. Environment issues

      • The command executable does not exist within the system (dependency not being installed). see prominc's answer
      • The command executable does not exist within a directory of those specified by PATH environment variable.
    2. Windows-only bugs/quirks

      • '.cmd' extension / shell: true. see li-zheng answer
      • Administrator permisions. see steve's answer
    3. Wrong spawn('command', ['--argument', 'list'], { cwd, env, ...opts }) usage

      • Specified working directory (opts.cwd) does not exist · see leeroy-brun's answer
      • Argument list within command String spawn('command --wrong --argument list')
      • Env vars within command string spawn('ENV_VAR=WRONG command')
      • Argument list Array specified as String spawn('cmd', '--argument list')
      • Unset PATH env variable spawn('cmd', [], { env: { variable } } => spawn('cmd', [], { env: { ...process.env, variable } }

    There are 2 posible origins for ENOENT:

    1. Code you are writing
    2. Code you depend on

    When origin is code you depend on, usual cause is an Environment Issue (or windows quirk)


    0 讨论(0)
  • 2020-11-22 03:33

    I was also going through this annoying problem while running my test cases, so I tried many ways to get across it. But the way works for me is to run your test runner from the directory which contains your main file which includes your nodejs spawn function something like this:

    nodeProcess = spawn('node',params, {cwd: '../../node/', detached: true });
    

    For example, this file name is test.js, so just move to the folder which contains it. In my case, it is test folder like this:

    cd root/test/
    

    then from run your test runner in my case its mocha so it will be like this:

    mocha test.js
    

    I have wasted my more than one day to figure it out. Enjoy!!

    0 讨论(0)
  • 2020-11-22 03:33

    I ran into this problem on Windows, where calling exec and spawn with the exact same command (omitting arguments) worked fine for exec (so I knew my command was on $PATH), but spawn would give ENOENT. Turned out that I just needed to append .exe to the command I was using:

    import { exec, spawn } from 'child_process';
    
    // This works fine
    exec('p4 changes -s submitted');
    
    // This gives the ENOENT error
    spawn('p4');
    
    // But this resolves it
    spawn('p4.exe');
    // Even works with the arguments now
    spawn('p4.exe', ['changes', '-s', 'submitted']);
    
    0 讨论(0)
  • 2020-11-22 03:36

    in windows, simply adding shell: true option solved my problem:

    incorrect:

    const { spawn } = require('child_process');
    const child = spawn('dir');
    

    correct:

    const { spawn } = require('child_process');
    const child = spawn('dir', [], {shell: true});
    
    0 讨论(0)
  • 2020-11-22 03:37

    @laconbass's answer helped me and is probably most correct.

    I came here because I was using spawn incorrectly. As a simple example:

    this is incorrect:

    const s = cp.spawn('npm install -D suman', [], {
        cwd: root
    });
    

    this is incorrect:

    const s = cp.spawn('npm', ['install -D suman'], {
        cwd: root
    });
    

    this is correct:

    const s = cp.spawn('npm', ['install','-D','suman'], {
        cwd: root
    });
    

    however, I recommend doing it this way:

    const s = cp.spawn('bash');
    s.stdin.end(`cd "${root}" && npm install -D suman`);
    s.once('exit', code => {
       // exit
    });
    

    this is because then the cp.on('exit', fn) event will always fire, as long as bash is installed, otherwise, the cp.on('error', fn) event might fire first, if we use it the first way, if we launch 'npm' directly.

    0 讨论(0)
  • 2020-11-22 03:38

    solution in my case

    var spawn = require('child_process').spawn;
    
    const isWindows = /^win/.test(process.platform); 
    
    spawn(isWindows ? 'twitter-proxy.cmd' : 'twitter-proxy');
    spawn(isWindows ? 'http-server.cmd' : 'http-server');
    
    0 讨论(0)
提交回复
热议问题