Grunt requires Node.js and npm to be installed. If you don’t have Node.js and/or npm installed on your machine, go to https://nodejs.org and download the installer or package for your operating system.
If you're installing Grunt for the first time, you'll first have to install the Grunt command-line interface package grunt-cli
globally.
npm install -g grunt-cli
This installs the command-line interface for Grunt globally so you can run the local version of Grunt in your project.
You can verify that you have grunt-cli
package installed by running the following command:
grunt --version
This should print at least the current version of your grunt-cli
package.
After you have grunt-cli
up and running, you can install the actual grunt
task runner and your first Grunt package grunt-contrib-jshint
:
npm install grunt --save-dev npm install grunt-contrib-jshint --save-dev
This downloads the packages from NPM package manager and saves them as devDependencies
to your package.json
file.
Next you need a Gruntfile.js
in your project root that acts as a config file for Grunt tasks:
module.exports = function(grunt) { grunt.initConfig({ jshint: { files: ['Gruntfile.js'], } }); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.registerTask('default', ['jshint']); };
This file does three things:
grunt-contrib-jshint
task from NPM packagejshint
task to run against the file Gruntfile.js
default
that runs the jshint
taskAfter you have set up your project you can run the default
task of Grunt by calling:
grunt
This fires up grunt-cli
that runs the local grunt
which looks for a Grunt task named default
which is configured to run the task called jshint
.