我有一个 JS 应用程序。它在 linux 上运行良好,但在 windows 10 中出现错误。
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
不正确的代码是这个
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()));
});
我在 github 中找到了这个错误的原因我想问题是产生 nodejs spawn Doc在 Windows 中无法正常工作。但我不知道如何修改这段代码以使其工作。有人可以帮我吗?
最佳答案
刚刚改了这一行
const child = spawn('npm', ['run', service]);
到这一行
const child = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['run', service]);
如果是 ti 的 windows 它运行 npm.cmd 如果它是 linux 则检查操作系统只是 npm
关于javascript - 错误 : spawn npm ENOENT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43230346/