Grunt is not
scary or hard

Created by Jackie Keiser

Grunt is a
javascript task runner.

It helps you be lazy automate repetitive tasks.

Automate all the things

What can Grunt do?

Grunt can take care of many of the repetitive tasks we do everyday.

Grunt can:

  • Compile your Sass
  • Reload your browser on file save
  • Compress your CSS and minify your JavaScript
  • Optimize your images
  • Build sprite files
  • And 3,385 other things...

Let's get started

Example Project

Grab a copy of the example project to work from.

Examples of the end result are in the examples folder within the project.

Install Grunt

  1. Install Node.js if you haven't already.
    ( Click the big green install button
  2. Install the Grunt.js command line interface
npm install -g grunt-cli

Note: This does not install the task runner. It just allows you to run multiple versions of Grunt. Grunt works more on a per project basis.

Yes, you have to use the command line.


It's fine. I promise.

Starting a project with Grunt

You'll need to setup these two files in the root of your project

Root Folder
  1. package.json - General info about your project and manages dependencies. To automatically generate this file run: npm init

  2. Gruntfile.js - The main setup file that contains the configuration of the tasks we install.


General info about your project and manages dependencies

  "name": "donuts-are-delicious",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-imagemin": "^0.8.0"

What else you could add to your package.json file


The main setup file that contains the configuration of the
tasks we install.

module.exports = function(grunt) {

    // 1. All configuration goes here
        pkg: grunt.file.readJSON('package.json'),

        // 2. Configuration for each task
        imagemin: {


    // 3. Where we tell Grunt we plan to use this plug-in.

    // 4. Where we tell Grunt what to do when we type "grunt" into the terminal.
    grunt.registerTask('default', ['imagemin']);


Install dependencies

Once you have your package.json file you can install all the dependencies you specified using the following command:

npm install
In the package.json example, we listed grunt and grunt-contrib-imagemin as our dependencies.

How to use a Grunt plugin

  1. Run command to install plugin
  2. Register task in Gruntfile.js
  3. Configure task
  4. Make sure it runs

1. Run command to install plugin

                        npm install grunt-contrib-sass --save-dev

The --save-dev flag means our package.json file gets automatically updated to include the dependency we've just installed

2. Register task in Gruntfile.js


This tells Grunt to load tasks from the specified Grunt plugin.

3. Configure tasks

    sass: {                                               // Task
        dist: {
            options: {
                style: 'expanded',                        // Output style
                sourcemap: true                           // Use source maps
            files: {
                'css/styles.css': 'scss/styles.scss'  // Output path : Sass path
    imagemin: {
        dynamic: {                                        // Another target
            files: [{
              expand: true,                               // Enable dynamic expansion
              cwd: 'examples/images/',                    // Src matches are relative to this path
              src: ['**/*.{png,jpg,gif}'],                // Actual patterns to match
              dest: 'images/'                             // Destination path prefix

In this example, we have the configuration for the sass and imagemin tasks.

4. Make sure it runs

grunt.registerTask('default', ['sass']);
grunt.registerTask('images', ['imagemin','sprite']);

In this example, running grunt in the command line without specifying a task will run the sass task. Running grunt images would run the imagemin and sprite task



Should we integrate Grunt into our workflow?