Why use a task runner?
In one word: automation. The less work you have to do when performing repetitive tasks like minification, compilation, unit testing, linting, etc, the easier your job becomes. After you’ve configured it through a Gruntfile, a task runner can do most of that mundane work for you—and your team—with basically zero effort.
Why use Grunt?
The Grunt ecosystem is huge and it’s growing every day. With literally hundreds of plugins to choose from, you can use Grunt to automate just about anything with a minimum of effort. If someone hasn’t already built what you need, authoring and publishing your own Grunt plugin to npm is a breeze. See how to get started.
Grunt and Grunt plugins are installed and managed via npm, the Node.js package manager. Grunt 0.4.x requires stable Node.js versions
>= 0.8.0. Odd version numbers of Node.js are considered unstable development versions.
Before setting up Grunt ensure that your npm is up-to-date by running
npm update -g npm (this might require
sudo on certain systems).
In order to get started, you’ll want to install Grunt’s command line interface (CLI) globally. You may need to use sudo (for OSX, *nix, BSD etc) or run your command shell as Administrator (for Windows) to do this.
npm install -g grunt-cli
This will put the
grunt command in your system path, allowing it to be run from any directory.
Note that installing
grunt-cli does not install the Grunt task runner! The job of the Grunt CLI is simple: run the version of Grunt which has been installed next to a
Gruntfile. This allows multiple versions of Grunt to be installed on the same machine simultaneously.
grunt is run, it looks for a locally installed Grunt using node’s
require() system. Because of this, you can run
grunt from any subfolder in your project.
If a locally installed Grunt is found, the CLI loads the local installation of the Grunt library, applies the configuration from your
Gruntfile, and executes any tasks you’ve requested for it to run. To really understand what is happening, read the code.
Assuming that the Grunt CLI has been installed and that the project has already been configured with a
package.json and a
Gruntfile, it’s very easy to start working with Grunt:
- Change to the project’s root directory.
- Install project dependencies with
- Run Grunt with
That’s really all there is to it. Installed Grunt tasks can be listed by running
grunt --help but it’s usually a good idea to start with the project’s documentation.