Node JS - child_process spawn('npm install') in Grunt task results in ENOENT error

gruntjs, node.js, npm

Solution

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

Problem

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

Original source

Related problems