Error: spawn npm ENOENT

I have a JS application. It works well on linux, but on Windows 10 I get 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 wrong code is

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 cause of this error on github. I assume that the problem arose with nodejs spawn Docs that do not work correctly in windows. But I do not know how to change this piece of code to make it work. Can anybody help me?

+15
source share
2 answers

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 checks the operating system if it starts npm.cmd in ti-windows, if it is linux just npm

+43
source

. MAC, Windows , spawn

command prompt

GIT bash , .

0

Source: https://habr.com/ru/post/1016324/


All Articles