Grunt: The JavaScript Task Runner

1.6.2 · active · verified Sun Apr 19

Grunt is a JavaScript task runner designed to automate repetitive development tasks such as minification, compilation, linting, and testing. It operates primarily through a command-line interface (CLI) and is configured declaratively via a `Gruntfile.js` located in a project's root. The current stable version is 1.6.2, requiring Node.js >=16. Grunt's release cadence is opportunistic, focusing on dependency updates, bug fixes, and maintaining compatibility with modern Node.js environments. Its key differentiators include a mature, stable architecture, a configuration-over-code paradigm that allows developers to define tasks and settings in a structured format, and a rich ecosystem of community-contributed plugins for diverse build processes. This declarative approach, contrasted with more programmatic build tools, makes Grunt accessible for automating complex workflows.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Grunt setup, including `package.json` for dependencies and a `Gruntfile.js` to define tasks for JavaScript minification and file watching. After `npm install`, running `grunt` executes the default `uglify` task, while `grunt dev` will minify and then watch for changes.

{
  "name": "my-project",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "^1.6.0",
    "grunt-contrib-uglify": "^5.0.0",
    "grunt-contrib-watch": "^1.1.0"
  }
}

// Gruntfile.js
module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'src/<%= pkg.name %>.js',
        dest: 'dist/<%= pkg.name %>.min.js'
      }
    },
    watch: {
      scripts: {
        files: ['src/**/*.js'],
        tasks: ['uglify'],
        options: {
          spawn: false,
        },
      },
    },
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.registerTask('default', ['uglify']);
  grunt.registerTask('dev', ['default', 'watch']);
};

view raw JSON →