How Do You Install and Configure Grunt?

Problem scenario
You want to use Grunt the automation tool (https://gruntjs.com).  How do you install it and get it to work on Linux?

Solution
#1  If you are using CentOS, Red Hat Enterprise Linux, or Fedora, run these two commands:

sudo yum -y install nodejs npm  # *
sudo npm install -g grunt

If you are using Debian or Ubuntu Linux, run this command:  sudo apt-get -y install nodejs npm grunt  # *

* If you need assistance installing Node.js and npm on a different distribution of Linux, see this posting.

2.a. Run this commmand:  sudo npm init

2.b.  # Respond with whatever you think is best.  You can press enter to these nine prompts:

name: (jdoe)
version: (1.0.0)
description:
entry point:
test command:
git repository:
keywords:
author:
license:

2.c.  For the "Is this ok?" prompt, choose "yes".

3.  Run these three commands:
sudo npm install -g grunt-cli
sudo npm install grunt --save-dev
sudo npm install grunt-contrib-concat --save-dev

4.  Create a file called Gruntfile.js with this as the content (the top line starts with "module.exports..."):

module.exports = function(grunt) {
    //based on https://stackoverflow.com/questions/15703598/
    // Project configuration.
    grunt.initConfig({
        concat: {
            "options": { "separator": ";" },
            "build": {
                "src": ["js/contint1.js", "js/contint2.js"],
                "dest": "js/completeapp.js"
            }
        }
    });

    // Load required modules
    grunt.loadNpmTasks('grunt-contrib-concat');

    // Task definitions
    grunt.registerTask('default', ['concat']);
};

5.  Run the command: grunt

You should see this:

'Running "concat:build" (concat) task

Done.'

6.  You can use this command to see what version of Grunt is installed:  grunt -V

Leave a comment

Your email address will not be published. Required fields are marked *