Tim Kelty
Tim Kelty

Reputation: 261

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

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.

Upvotes: 10

Views: 1323

Answers (2)

Tim Kelty
Tim Kelty

Reputation: 261

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!

Upvotes: 2

vanadium23
vanadium23

Reputation: 3586

After update #2, I have more options to write why this combination don't work/ First of all, we need to figure out which user a execute the commands. To do this, you need to execute command whoami and see result (you can do it through ssh or shipit task). The direct question is what priveleges don't have user to install packages.

Possible solution #1:

User don't have rights on tmp folder. We can check it via ls -ld /path/to/tmp, expected output:

drwxrwxrwt 13 root root 280 May  8 19:42 /tmp

You need to verify, that all users can write in tmp dir: drwxrwxrwx. If not, you can set by chmod -R 755.

Possible solution #2:

Execute on npm is granted only on owner user/group, and our user don't have ones. We can check it via ls -lL $(which npm). Expected output:

-rwxr-xr-x 1 root root 1881 Apr 19 07:12 /usr/../lib/node_modules/npm/bin/npm-cli.js

If there is no permission x on file, then execute sudo chmod +x $(which npm).

UPDATE: After this question there is might be also problem not with your tmp folder, but with debug folder for npm. Try to search for one & check permission.

Hope it's all options that we can met.

Upvotes: 0

Related Questions