Node.js Installing gruntplugins


Example

Adding dependcy

To use a gruntplugin, you first need to add it as a dependency to your project. Let's use the jshint plugin as an example.

npm install grunt-contrib-jshint --save-dev

The --save-dev option is used to add the plugin in the package.json, this way the plugin is always installed after a npm install.

Loading the plugin

You can load your plugin in the gruntfile file using loadNpmTasks.

grunt.loadNpmTasks('grunt-contrib-jshint');

Configuring the task

You configure the task in the gruntfile adding a property called jshint to the object passed to grunt.initConfig.

grunt.initConfig({
  jshint: {
    all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
  }
});

Don't forget you can have other properties for other plugins you are using.

Running the task

To just run the task with the plugin you can use the command line.

grunt jshint

Or you can add jshint to another task.

grunt.registerTask('default', ['jshint']);

The default task runs with the grunt command in the terminal without any options.