Search code examples
yeomanyeoman-generator

yeoman: automatically install npm modules


I've created a yeoman generator.

Once I've created a project scaffold using my generator, I run the command "npm install grunt-contrib-watch grunt-contrib-connect"

I've been following the doc here: http://yeoman.io/generators.html

It seems to indicate that npm dependencies can be installed automatically. But I can't quiet figure out how to make this happen.

So, how do I define npm depencies in my generator, so that the node modules get installed automatically when the scaffold gets created?


Solution

  • You can generate a package.json as part of your scaffolding just as any other file. It can even contain template markup, as in generator-webapp.

    If you scaffold out a package.json in the root directory of your generated project, you can run installDependencies() of the generator object at the end:

    this.on('end', function () {
      this.installDependencies();
    });
    

    This will take care of installing bower and npm dependencies that the project declares.