nodejs在spawn中执行npm报错 [Error: spawn ENOENT]” errors

时间:2021-12-17 20:56:28

描述: 上代码

var ps = require('child_process').spawn("npm", ['install'], {
    stdio: 'inherit',
    cwd: srcPath
});

ps.on('error', function(err) {
    console.log(err);
});

ps.on('exit', function (err) {
    console.log('exit');
});

执行报[Error: spawn ENOENT]

 

原因,windows下npm执行名不同

解决方案

var ps = require('child_process').spawn(process.platform === "win32" ? "npm.cmd" : "npm", ['install'], {
    stdio: 'inherit',
    cwd: srcPath
});

参考:http://*.com/questions/17516772/using-nodejss-spawn-causes-unknown-option-and-error-spawn-enoent-err