10

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

zedd45
  • 2,101
  • 1
  • 31
  • 34

1 Answers1

13

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 });
Kyle Robinson Young
  • 13,732
  • 1
  • 47
  • 38
  • 1
    That worked perfectly. I can't believe how easy / obvious the solution was once you provided it, or how hard it eluded me. Thanks so much! – zedd45 Nov 25 '13 at 14:35
  • 2
    for future reference of possible visitors, http://stackoverflow.com/questions/27688804/how-to-debug-any-node-js-child-process-error-spawn-enoent – laconbass Dec 29 '14 at 13:26
  • Hmm, this didn't work for me. Still getting a spawnSync npm ENOENT (yes I'm using the sync version) – ianbeks Mar 22 '16 at 16:55
  • 6
    On Windows you need to call `npm.cmd`. – Kai Sellgren Apr 08 '19 at 10:53