Yeoman Generator: perform automatic npm install

Yeoman TL;DR;

Yeoman is an awesome tool. In short words, it is a scaffolding tool to get you set up quickly with a new project by asking you only a few questions. Not only it's widespread and has a large community, but also provides a nice API for devs to create new generators (those are the installable packages).

If you haven't checked it out so far head over to their Getting Started page or even better the great step-by-step codelab.

Automating npm install

So much for the intro now lets get to reason for this post. I've created a simple Generator for the Aurelia framework which essentially gets you the latest sources from GitHub and installs those. Additionally you get some simple scaffolding features for VM's and Views.

After that process the user is supposed to follow the usual Aurelia process and perform the npm install and jspm install commands. So a recent user request was about automating those. After some research it turns out that Yeomans API provides a really helpful method called npmInstall.

For those of you who haven't worked with Yeoman, it's pretty simple. You create your generator by extending the yeoman-generator Base class (1). Afterwards each function included will be executed sequentially so you get a chance to nicely structure your code (2). So in order to install a specific module we just need to create a method with following code shown in section (3). Alternatively you can call the method npmInstall without any parameters to simulate the desired installation of all dependencies mentioned in the package.json file (4).

// (1): Create a generator by extending the Base class
  // (2): Create a function with any name to add a step
  installJquery: function() {
    // (3): Install jQuery via npm
    this.npmInstall(['jquery'], { 'saveDev': true });

  installAllFromPackageJSON: function() {
    // (4): Install all dependencies from package.json


As depicted in the example above, working with Yeoman's API is straightforward and should get you started quickly with your next CLI / Scaffolding Project.

The next step to finish the request is to find out how to automate JSPM install. Let's see whether that works equally easy :)

photo credit: Automatic Transmission via photopin (license)