LinuxCommandLibrary

grunt

Automate repetitive JavaScript tasks

TLDR

Run the default task process

$ grunt
copy

Run one or more tasks
$ grunt [task1 task2 ...]
copy

Specify an alternative configuration file
$ grunt --gruntfile [path/to/file]
copy

Specify an alternative base path for relative files
$ grunt --base [path/to/directory]
copy

Specify an additional directory to scan for tasks in
$ grunt --tasks [path/to/directory]
copy

Perform a dry-run without writing any files
$ grunt --no-write
copy

Display help
$ grunt --help
copy

SYNOPSIS

grunt [options] [task [task ...]]

PARAMETERS

--help, -h
    Print help information and exit

--version, -V
    Print Grunt version and exit

--base, -b DIR
    Change to directory DIR before processing

--gruntfile, -g FILE
    Use specific Gruntfile instead of default (Gruntfile.js, etc.)

--debug, -d
    Enable debug mode and print stack traces on failure

--force, -f
    Continue execution even if tasks fail

--verbose, -v
    Enable verbose logging (repeat for more detail)

--color, -c
    Force colored output (--no-color to disable)

DESCRIPTION

Grunt is a popular JavaScript task runner designed to automate repetitive tasks in web development projects. It excels at streamlining workflows such as minifying files, compiling Sass/Less to CSS, linting code with JSHint or ESLint, running unit tests with Mocha or Jasmine, and concatenating JavaScript files.

To use Grunt, first install Node.js and npm. Then, globally install the Grunt CLI with npm install -g grunt-cli. In a project, add Grunt as a dev dependency via npm install grunt --save-dev and create a Gruntfile.js (or CoffeeScript equivalent) to define tasks using a simple API. Tasks are registered with grunt.registerTask() or loaded from plugins via grunt.loadNpmTasks().

Invoke Grunt from the command line to run default tasks or specify targets. Its plugin ecosystem is vast, with thousands available on npm for tasks like uglification (grunt-contrib-uglify), watching files (grunt-contrib-watch), and live reloading (grunt-contrib-connect). While powerful, Grunt has been somewhat superseded by faster alternatives like Gulp.js due to its configuration-over-code approach generating many temporary files.

Grunt is cross-platform, including Linux, but requires manual installation as it's not a native Unix tool.

CAVEATS

Requires Node.js and npm installation; not a native Linux command. Gruntfile must exist in project root or specified path. Large projects may generate many temp files.

INSTALLATION

Global CLI: npm install -g grunt-cli
Project: npm install grunt --save-dev
Create Gruntfile.js for tasks.

BASIC GRUNTFILE EXAMPLE

module.exports = function(grunt) {
  grunt.registerTask('default', ['jshint', 'uglify']);
};

HISTORY

Created by Ben Alman in 2012 as a task runner for Node.js. Grunt CLI released separately for global access. Reached version 1.0 in 2016 with major API simplifications. Peaked in popularity around 2014-2016 before streaming alternatives like Gulp gained traction.

SEE ALSO

npm(1), node(1), make(1)

Copied to clipboard