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

后端 未结 25 2646
轻奢々
轻奢々 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: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.

提交回复
热议问题