在Grunt任务中的节点JS - child_process spawn('npm install')导致ENOENT错误 [英] Node JS - child_process spawn('npm install') in Grunt task results in ENOENT error

查看:663
本文介绍了在Grunt任务中的节点JS - child_process spawn('npm install')导致ENOENT错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创作一个Grunt任务时遇到了一些困难。我尝试执行npm install,然后是bower install,然后是一个grunt中心目标(触发多个子项目的构建命令)。



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

  var path = require('path'),
projectPath = path.resolve(process.cwd(),this.data.activity);

grunt.log.debug('project path calculated 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('出错了,为'+ path +'安装deps错误: ',data);
});

spawnProcess.on('close',function(exitCode){

console.log('ls已经结束退出代码:'+ exitCode);
});

当前的代码(用ls代替npm install)会导致:

 运行install:projects(安装)任务[D]任务源:/Users/zedd45/proj/Gruntfile.js 
验证属性安装.projects存在于配置中... OK
文件:[no files]
[D]项目路径计算方式为:/ Users / zedd45 / proj / activity / web / client
current dir is :/ Users / zedd45 / proj / activity / web / client
EVN(简称)是:{
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已完成Exit代码:0

但是如果我将'ls'更改为'npm install' b``致命错误:产生ENOENT



紧随其后



我试过chmod 777的目录,这似乎没有帮助。



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

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

前者的结果是


警告:Object#没有方法'slice'使用--force到
continue。


以后仍然会导致ENOENT错误。



任何与ENOENT错误有关的帮助都可能对您有帮助。使用Google搜寻它,或使用子流程API文档,我还没有取得太大的成功。

解决方案

再次检查child_process.spawn上的文档。第一个参数应该是 only 要运行的命令,第二个参数是参数:

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


I'm having some difficulty with a Grunt task I'm authoring. I'm trying to execute npm install, followed by bower install, followed by a grunt hub target (to trigger a build command for multiple sub-projects).

The problem I'm encountering lies with child_process. I get spawn ENOENT error if I run the following commands in my grunt task, with the npm install spawn command that's currently commented out:

    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);
        });

the current code (with ls instead of npm install) results in:

   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

but if I change 'ls' to 'npm install' I get instead ``Fatal error: spawn ENOENT

immediately following the ENV print.

I have tried chmod 777 for that directory, which doesn't seem to help.

I have also tried:

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

and

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

The former results in

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

the later still results in the ENOENT error.

Any help with exactly what this ENOENT error is would probably help a great deal; I haven't had much success with Googling it nor with the child process API docs

解决方案

Double check the docs on child_process.spawn again. The first argument should be only the command to run and the second is the arguments:

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

这篇关于在Grunt任务中的节点JS - child_process spawn('npm install')导致ENOENT错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆