我在创作Grunt任务时遇到了一些困难。我正在尝试执行npm install,然后执行bower install,然后执行grunt hub目标(触发多个子项目的构建命令)。
我遇到的问题在于child_process。如果我在我的grunt任务中运行以下命令,并且当前已注释掉的npm install spawn命令,则会出现生成ENOENT错误:
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 install)导致:
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});
前者导致
警告:对象#没有方法'slice'使用--force to 继续。
后者仍会导致ENOENT错误。
任何有关ENOENT错误的帮助可能会有很大帮助;我使用Google搜索和child process API docs
都没有取得多大成功答案 0 :(得分:10)
再次仔细检查child_process.spawn上的文档。第一个参数应该是仅要运行的命令,第二个参数应该是参数:
var npm = spawn('npm', ['install'], { cwd: projectPath });