在Grunt任务中的节点JS – child_process spawn('npm install')导致ENOENT错误

我正在创作一个Grunt任务。 我试图执行npm install,然后是bower安装,然后是一个咕hub中心目标(触发多个子项目的构build命令)。

我遇到的问题与child_process谎言。 我得到产卵ENOENT错误,如果我在我的grunt任务中运行以下命令,使用当前注释掉的npm install spawn命令:

var path = require('path'), projectPath = path.resolve(process.cwd(), this.data.activity ); grunt.log.debug('project path computed as: ', projectPath); process.chdir( projectPath ); console.log('current dir is: ', process.cwd()); console.log('EVN is: ', process.env); var spawnProcess = spawn('ls'); // var spawnProcess = spawn('npm install'); spawnProcess.stdout.on('data', function (data) { console.log('' + data); }); spawnProcess.stderr.on('data', function(data) { console.log('something went wrong installing deps for ' + path + '. Error: ', data); }); spawnProcess.on('close', function (exitCode) { console.log( 'ls has finished with Exit Code: ' + exitCode); }); 

当前的代码(用ls而不是npm安装)导致:

  running "install:projects" (install) task[D] Task source: /Users/zedd45/proj/Gruntfile.js Verifying property install.projects exists in config...OK File: [no files] [D] project path computed as: /Users/zedd45/proj/activity/web/client current dir is: /Users/zedd45/proj/activity/web/client EVN (abbreviated) is: { TERM_PROGRAM: 'iTerm.app', SHELL: '/bin/bash', PWD: '/Users/zedd45/proj', ... OLDPWD: '/Users/zedd45/proj/activity/web/client', _: '/usr/local/bin/grunt' } GruntFile.js bower.json package.json this_is_the_directory_you_are_looking_for.txt ls has finished with Exit Code: 0 

但是如果我把'ls'改成'npm install',我会得到“致命的错误:产生ENOENT

紧随ENV印刷之后。

我已经尝试了该目录的chmod 777,这似乎没有帮助。

我也试过了:

 // var spawnProcess = spawn('npm install', {'cwd': projectPath}); 

 // var spawnProcess = spawn('npm install', [], {'cwd': projectPath}); 

前者的结果是

警告:对象#没有方法“切片”使用 – 强制继续。

后者仍然会导致ENOENT错误。

任何与ENOENT错误有关的帮助都可能有助于很多事情; 我没有太多的成功谷歌search它,也没有儿童进程的API文档

Solutions Collecting From Web of "在Grunt任务中的节点JS – child_process spawn('npm install')导致ENOENT错误"