Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

child_process.exec/spawn triggers callback/close with npm install command (via Gulp / Shipit)

  • I'm using Shipit for deployment.
  • On deploy, Shipit checks out the current Git Sha, to a tmp directory, then I run npm install followed by gulp build, then proceed with the deploy.
  • Shipit uses Orchestrator for it's task flow, as Gulp does.
  • Shipit has it's own CLI, so I can deploy with shipit development deploy.

Everything thing above works. What I'm trying to do is create a gulp deploy task that will initialize Shipit directly, instead of using the CLI. Looks something like this:

gulp.task('shipit:deploy', function() {
  var deployToEnv = argv['deploy-to'] || false;
  var shipit;
  return inquirer.prompt([{
    type: 'list',
    name: 'deployToEnv',
    default: deployToEnv,
    message: 'Deploy to environment:',
    choices: envs
  }]).then(function(answers) {
    deployToEnv = answers.deployToEnv;
    shipit = new Shipit({environment: deployToEnv});
    shipit.initialize();
    shipit.start('deploy');
  });
});

Corresponding shipit config:

  shipit.initConfig(config);
  shipit.blTask('build', function() {
    return shipit.local('npm install --silent', {
      cwd: shipit.config.workspace
    }).then(function() {
      return shipit.local('gulp build', {
        cwd: shipit.config.workspace
      });
    });
  });

  shipit.on('fetched', function() {
    shipit.start('build');
  });

Things appear to work with one problem: it doesn't actually perform the npm install!

Running "npm install --silent" on local. Running "gulp build" on local.

So, it would seem something in the npm install command is prematurely resolving the promise, but I'm not sure how or why.

I had a similar problem (just using shipit cli) with npm warnings, which is when I discovered using the --silent arg solved that.

As a test, I left the code as is, but replaced npm install --silent with sleep 10. Sure enough, it waited for 10 seconds before executing gulp build. So, it would seem it is something specific with the npm install command.

Any help is appreciated!

Update #1:

shipit.local uses child_process.exec. I tried converting this to use child_process.spawn, but had the same result.

Update #2:

If I change the command to sudo npm install, things work as expected! So...what does this mean, and how can I avoid running it with sudo?

Update #3:

Still unable to do this without sudo, but I tried adding the --verbose flag with these results:

Without sudo:

@ npm info it worked if it ends with ok
@ npm verb cli [ '/Users/timkelty/.nvm/versions/node/v0.12.0/bin/node',
@ npm verb cli   '/Users/timkelty/.nvm/versions/node/v0.12.0/bin/npm',
@ npm verb cli   'install',
@ npm verb cli   '--verbose' ]
@ npm info using [email protected]
@ npm info using [email protected]
@ npm verb install where, deps [ '/Users/timkelty/tmp/edwards-garment-website', [] ]
@ npm verb install where, peers [ '/Users/timkelty/tmp/edwards-garment-website', [] ]
@ npm info preinstall [email protected]
@ npm info build /Users/timkelty/tmp/edwards-garment-website
@ npm verb linkStuff [ false, false, false, '/Users/timkelty/tmp' ]
@ npm info linkStuff [email protected]
@ npm verb linkBins [email protected]
@ npm verb linkMans [email protected]
@ npm verb rebuildBundles [email protected]
@ npm info install [email protected]
@ npm info postinstall [email protected]
@ npm verb exit [ 0, true ]
@ npm info ok
Running "gulp build" on local.
@ [15:14:32] Local gulp not found in ~/tmp/edwards-garment-website
@ [15:14:32] Try running: npm install gulp
'build' errored after 755 ms
Error: Command failed: /bin/sh -c gulp build

With sudo:

Running "sudo npm install --verbose" on local.
@ npm info it worked if it ends with ok
@ npm verb cli [ '/Users/timkelty/.nvm/versions/node/v0.12.0/bin/node',
@ npm verb cli   '/Users/timkelty/.nvm/versions/node/v0.12.0/bin/npm',
@ npm verb cli   'install',
@ npm verb cli   '--verbose' ]
@ npm info using [email protected]
@ npm info using [email protected]
@ npm verb install where, deps [ '/Users/timkelty/tmp/edwards-garment-website',
@ npm verb install   [ 'Select2',
@ npm verb install     'autoprefixer-core',
@ npm verb install     'babel',

...so for some reason, when running without sudo, the npm install command seems to think it doesn't have any dependencies to install, so it finishes without error and continues to my next task.

like image 660
Tim Kelty Avatar asked May 01 '15 14:05

Tim Kelty


1 Answers

I found the solution!

The problem was that I was trying to install devDependencies, but when I ran the gulp deploy command, NODE_ENV was set to production, which does not install devDependencies.

Therefore, changing the command to NODE_ENV=development npm install seems to fix it!

like image 140
Tim Kelty Avatar answered Sep 29 '22 00:09

Tim Kelty