45

I have an JS app. It works good on linux but in windows 10 I am getting an error.

events.js:161
  throw er; // Unhandled 'error' event
  ^

Error: spawn npm ENOENT
    at exports._errnoException (util.js:1028:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:359:16)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
    at Module.runMain (module.js:607:11)
    at run (bootstrap_node.js:422:7)
    at startup (bootstrap_node.js:143:9)
    at bootstrap_node.js:537:3

and the code which is incorrect is this

const spawn = require('child_process').spawn;

const watching = [
  // {service: "babel-watch"},
  {service: "webpack-watch"},
  // {service: "sass-watch"},
  {service: "server-watch"}
];

watching.forEach(({service}) => {
  const child = spawn('npm', ['run', service]);
  child.stdout.on('data', d => console.log(d.toString()));
  child.stderr.on('data', d => console.log(d.toString()));
});

I found the reason of this error in github I guess the problem is spawn nodejs spawn Doc which have didn't work correctly in windows. But I don't know how to modify this snippet of code to make it work. Can someone help me ?

Armen Sanoyan
  • 1,898
  • 2
  • 19
  • 32

4 Answers4

88

Just changed this line

const child = spawn('npm', ['run', service]);

to this line

  const child = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['run',  service]);

Which is checking the operating system if ti's windows it runs npm.cmd if it's linux just npm

Armen Sanoyan
  • 1,898
  • 2
  • 19
  • 32
6

I know there is a correct answer and this question has been around for a long time, my solution is based on the answer @Armen Sanoyan and How do I determine the current operating system with Node.js

For me the @Armen Sanoyan answer does not work, but help a lot. I changed for this line and work.

const child = (process.platform === 'win32' ? 'npm.cmd' : 'npm') + ' run ' + service;

I hope I can help.

Cava
  • 5,346
  • 4
  • 25
  • 41
3

I know this seems obvious but make sure you're not putting several flags/options into a single string:

// do not do this
spawn('pnpm', ['-r', 'exec', '--', 'pnpm version patch'])

// do this
spawn('pnpm', ['-r', 'exec', '--', 'pnpm', 'version', 'patch'])
Ryan Wheale
  • 26,022
  • 8
  • 76
  • 96
1

I faced the same problem. My application code was running fine in MAC but in Windows it was giving an error about the code related to the spawn command

The error occurs when running using command prompt

When I used GIT bash to start the application then the error did not occur. I did not need to change any code

firstpostcommenter
  • 2,328
  • 4
  • 30
  • 59
  • I had to also delete the favicon.ico file from code repository because I was unable to start the application due to some error when this file is present in my local windows machine – firstpostcommenter Oct 08 '19 at 08:22