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

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

    Add C:\Windows\System32\ to the path environment variable.

    Steps

    1. Go to my computer and properties

    2. Click on Advanced settings

    3. Then on Environment variables

    4. Select Path and then click on edit

    5. Paste the following if not already present: C:\Windows\System32\

    6. Close the command prompt

    7. Run the command that you wanted to run

    Windows 8 Environment variables screenshot

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

    Are you changing the env option?

    Then look at this answer.


    I was trying to spawn a node process and TIL that you should spread the existing environment variables when you spawn else you'll loose the PATH environment variable and possibly other important ones.

    This was the fix for me:

    const nodeProcess = spawn('node', ['--help'], {
      env: {
        // by default, spawn uses `process.env` for the value of `env`
        // you can _add_ to this behavior, by spreading `process.env`
        ...process.env,
        OTHER_ENV_VARIABLE: 'test',
      }
    });
    
    0 讨论(0)
  • 2020-11-22 03:28

    I ran into the same problem, but I found a simple way to fix it. It appears to be spawn() errors if the program has been added to the PATH by the user (e.g. normal system commands work).

    To fix this, you can use the which module (npm install --save which):

    // Require which and child_process
    const which = require('which');
    const spawn = require('child_process').spawn;
    // Find npm in PATH
    const npm = which.sync('npm');
    // Execute
    const noErrorSpawn = spawn(npm, ['install']);
    
    0 讨论(0)
  • 2020-11-22 03:28

    Use require('child_process').exec instead of spawn for a more specific error message!

    for example:

    var exec = require('child_process').exec;
    var commandStr = 'java -jar something.jar';
    
    exec(commandStr, function(error, stdout, stderr) {
      if(error || stderr) console.log(error || stderr);
      else console.log(stdout);
    });
    
    0 讨论(0)
  • 2020-11-22 03:29

    As @DanielImfeld pointed it, ENOENT will be thrown if you specify "cwd" in the options, but the given directory does not exist.

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

    For anyone who might stumble upon this, if all the other answers do not help and you are on Windows, know that there is currently a big issue with spawn on Windows and the PATHEXT environment variable that can cause certain calls to spawn to not work depending on how the target command is installed.

    0 讨论(0)
提交回复
热议问题