2013-11-22 50 views
6

我在創作一個Grunt任務時遇到了一些困難。我試圖執行npm install,然後是bower install,然後是一個grunt中心目標(爲多個子項目觸發構建命令)。節點JS - child_process spawn('npm install')在Grunt任務中導致ENOENT錯誤

我遇到的問題與child_process謊言。我得到重生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安裝)結果:

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」到「故宮安裝」我得到,而不是 ``致命錯誤:產卵ENOENT

立即ENV打印以下。

我已經嘗試chmod 777該目錄,這似乎沒有幫助。

我也曾嘗試:

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

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

前者導致

Warning: Object # has no method 'slice' Use --force to continue.

後仍導致ENOENT錯誤。

任何幫助確切地說這個ENOENT錯誤可能會幫助很多;我沒有太多的成功谷歌搜索它與child process API docs

回答

7

再次檢查child_process.spawn上的文檔。第一個參數應該是運行命令,第二個是參數:

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

那完美。我無法相信一旦你提供瞭解決方案,解決方案是多麼的簡單/明顯,或者它難以避免。非常感謝! – zedd45

+2

爲可能的訪問者的未來參考,http://stackoverflow.com/questions/27688804/how-to-debug-any-node-js-child-process-error-spawn-enoent – laconbass

+0

嗯,這不適用於我。仍然得到一個spawnSync npm ENOENT(是的,我正在使用同步版本) – ianbeks