Managing Dependencies

Once you've run your generators, you'll often want to run npm (or Yarn) and Bower to install any additional dependencies your generators require.

As these tasks are very frequent, Yeoman already abstracts them away. We'll also cover how you can launch installation through other tools.

Note that Yeoman provided installation helpers will automatically schedule the installation to run once as part of the install queue. If you need to run anything after they've run, use the end queue.

npm

You just need to call generator.npmInstall() to run an npm installation. Yeoman will ensure the npm install command is only run once even if it is called multiple times by multiple generators.

For example you want to install lodash as a dev dependency:

class extends Generator {
  installingLodash() {
    this.npmInstall(['lodash'], { 'save-dev': true });
  }
}

This is equivalent to call:

npm install lodash --save-dev

on the command line in your project.

Yarn

You just need to call generator.yarnInstall() to launch the installation. Yeoman will ensure the yarn install command is only run once even if it is called multiple time by multiple generators.

For example you want to install lodash as a dev dependency:

generators.Base.extend({
  installingLodash: function() {
    this.yarnInstall(['lodash'], { 'dev': true });
  }
});

This is equivalent to call:

yarn add lodash --dev

on the command line in your project.

Bower

You just need to call generator.bowerInstall() to launch the installation. Yeoman will ensure the bower install command is only run once even if it is called multiple time by multiple generators.

Combined use

Calling generator.installDependencies() runs npm and bower by default. You can decide which ones to use be passing booleans for each package manager.

Example for using Yarn with Bower:

generators.Base.extend({
  install: function () {
    this.installDependencies({
      npm: false,
      bower: true,
      yarn: true
    });
  }
});

Using other tools

Yeoman provides an abstraction to allow users to spawn any CLI commands. This abstraction will normalize to command so it can run seamlessly in Linux, Mac and Windows system.

For example, if you're a PHP aficionado and wished to run composer, you'd write it this way:

class extends Generator {
  install() {
    this.spawnCommand('composer', ['install']);
  }
}

Make sure to call the spawnCommand method inside the install queue. Your users don't want to wait for an installation command to complete.

Found a typo? An unclear example? Help us improve our documentation by forking and sending your fixes and suggestions. Improve this Page!