I have an JS app. It works good on linux but in windows 10 I am getting an error.
JavaScript
x
15
15
1
events.js:161
2
throw er; // Unhandled 'error' event
3
^
4
5
Error: spawn npm ENOENT
6
at exports._errnoException (util.js:1028:11)
7
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
8
at onErrorNT (internal/child_process.js:359:16)
9
at _combinedTickCallback (internal/process/next_tick.js:74:11)
10
at process._tickCallback (internal/process/next_tick.js:98:9)
11
at Module.runMain (module.js:607:11)
12
at run (bootstrap_node.js:422:7)
13
at startup (bootstrap_node.js:143:9)
14
at bootstrap_node.js:537:3
15
and the code which is incorrect is this
JavaScript
1
15
15
1
const spawn = require('child_process').spawn;
2
3
const watching = [
4
// {service: "babel-watch"},
5
{service: "webpack-watch"},
6
// {service: "sass-watch"},
7
{service: "server-watch"}
8
];
9
10
watching.forEach(({service}) => {
11
const child = spawn('npm', ['run', service]);
12
child.stdout.on('data', d => console.log(d.toString()));
13
child.stderr.on('data', d => console.log(d.toString()));
14
});
15
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 ?
Advertisement
Answer
Just changed this line
JavaScript
1
2
1
const child = spawn('npm', ['run', service]);
2
to this line
JavaScript
1
2
1
const child = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['run', service]);
2
Which is checking the operating system if ti’s windows it runs npm.cmd if it’s linux just npm