9

After generating my project scaffolding, I'd like Yeoman to install my npm dependencies in a subfolder, rather than in the main project folder. I have my package.json file in the /gulp subfolder of my project. How can I have Yeoman install the dependencies there? Here is my current function that runs at the end of the generator:

this.on('end', function () {
  if (!this.options['skip-install']) {
    this.installDependencies({
      bower: false,
      npm: true
    });
  }
});
bzw
  • 253
  • 2
  • 6

1 Answers1

16

Finally got this working by changing the directory before running this.installDependencies() in index.js, as in:

this.on('end', function () {
  if (!this.options['skip-install']) {

    // Change working directory to 'gulp' for dependency install
    var npmdir = process.cwd() + '/gulp';
    process.chdir(npmdir);

    this.installDependencies({
      bower: false,
      npm: true
    });
  }
});

Hope this helps if you have a different project scaffolding setup.

bzw
  • 253
  • 2
  • 6
  • 6
    Alternatively, you could also run any shell command (*nix or windows) using `this.spawnCommand("npm", ["install"], { cwd: 'scripts'})` where cwd points to the directory you wish to run your command from. – Kirill G. May 08 '14 at 15:22
  • @KirillG.'s solution works for me. However, a package-lock.json was created in the current directory. Any ideas on why this is happening? This is in the package-lock.json `{ "lockfileVersion": 1 }` – Vien Tang May 31 '19 at 17:07
  • @VienTang In order to avoid creating a package-lock on the root try the following ```javascript this.npmInstall([], { prefix: 'scripts' }); ``` – HYMXDEV Feb 19 '21 at 18:10