Grunt is a JavaScript task runner used to automate tasks such as minifying assets, compilation and other repetitive tasks. Getting started with Grunt is easy: simply install the CLI, create a package.json and a Gruntfile file, and run the Grunt command.

To install the CLI on your machine and allow the use of running the grunt command, run the below command in Terminal. (Note that you may need to run the command using administrator privileges).

$ npm install -g grunt-cli

A Grunt project should have two files: package.json, which lists the project’s dependencies, and a Gruntfile which will either be a Gruntfile.js or Gruntfile.coffee file. This file will be used to define tasks and load plugins.

The configuration for each task belongs in grunt.initConfig() method. The build property specifies the src, which is the location of the files to be minified, and dest, the destination of the minified file.

The banner option, prepends a string to the minified file. So in this example, the minified file would contain this string at the top of the file:

/*! my-grunt-project 2017-08-06 */

In order to run tasks, the plugins must be loaded. Hence a grunt.loadNpmTasks('grunt-contrib-uglify'); line is added.

The task itself can be run by simply running grunt in the command line. This is because the uglify task has been added as a default task using:

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

If it had not been configured as a default task, the task would also have to be specified in the command.

$ grunt uglify

When the command has been run, a minified JavaScript file will be created within your project’s js/ directory.

Should you need to log information using Grunt, you can use grunt.log.write().

grunt.log.write('Some logging information')

This information would then be output in the Terminal when running the grunt command.

There are many more plugins to be used with Grunt, such as JSLint and LESS to CSS compilation. Further plugins can be easily installed by running npm install <package-name> in your project’s directory, loading the task using grunt.loadNpmTasks() within your Gruntfile, and finally, adding the configuration within grunt.initConfig().