Install Grunt and Grunt-cli on Ubuntu 16.04

Install Grunt

Introduction

Grunt is a JavaScript task runner, which helps in performing repetitive tasks like minification, compilation, unit testing, and linting. This means that a developer can save time and spend it doing more useful development work.
It can be configured through Gruntfiles, and extended with plugins.
This tutorial explains how to install Grunt and Grunt-cli on Ubuntu 16.04.

Getting started

Grunt and Grunt plugins are installed and managed via the Node.js package manager, npm. So, the first thing to do is install Node.js.
Node.js is available in the Ubuntu repository, with its required dependencies:

# apt install nodejs npm

If a more recent version is needed, there is a PPA, maintained by NodeSource; in this case:

$ cd ~
$ curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh

then, execute the downloaded script:

# bash nodesource_setup.sh

This will set up the repository. Once this process is complete, insert the following code:

# apt install nodejs

In this case, it’s not necessary to install npm separately.
Check that the nodejs version is 6.x.x:

$ nodejs --version

and npm 3.x.x:

$ npm --version

Installing Grunt and Grunt-CLI

Check if npm is up to date:

# npm update -g npm

This should update npm to a 4.x.x version. At this point, it’s possible to install Grunt and its CLI:

# npm -g install grunt
# npm -g install grunt-cli

The job of the Grunt CLI is to run the version of Grunt which has been installed next to a Gruntfile. This allows multiple versions of Grunt to be installed on the same machine simultaneously.

Testing Grunt

A Grunt setup involves adding two files to a project:

  • package.json: used by npm to store metadata for projects published as npm modules. Grunt and the Grunt plugin ‘project needs’ will be listed as ‘devDependencies’ in this file.
  • Gruntfile: named Gruntfile.js or Gruntfile.coffee and is used to configure or define tasks and load Grunt plugins.

Create a new project

First, create a new folder, like this:

$ mkdir ~/grunt_project

In this folder, execute:

$ npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install  --save` afterwards to install a package and
save it as a dependency in the package.json file.

Then, enter the required information, like project name, author, a git repository, version, etc.
At the end of this process:

About to write to /home/gmolica/grunt_project/package.json:

{
  "name": "grunt_project",
  "version": "0.0.1",
  "description": "This is a Unixmen tutorial.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Unixmen"
  ],
  "author": "Giuseppe Molica",
  "license": "ISC"
}


Is this ok? (yes) yes

Press Enter and it will create a package.json file with all of the information you entered.

Add Grunt to the project

Next, it’s time to add Grunt to this project. The easiest way is:

$ npm install grunt --save-dev

This command will write into package.json a devDependencies block, like:

"devDependencies": {
    "grunt": "^1.0.1"
  }

It will also create a new folder, named node_modules containing all required Grunt modules.
The next thing to do is, of course, create the Gruntfile. It is a JavaScript or CoffeeScript file that belongs in the root directory of the project, and that should be committed with the project source.
That file is comprised of the following parts:

  • The “wrapper” function
  • Project and task configuration
  • Loading Grunt plugins and tasks
  • Custom tasks

Let’s see an example of Gruntfile.js:

module.exports = function(grunt) { /*This is the wrapper function*/

  /* Project configuration */
  grunt.initConfig({ 
    pkg: grunt.file.readJSON('package.json'),
    uglify: { //uglify is a Grunt plugin used in this example
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'src/<%= pkg.name %>.js',
        dest: 'build/<%= pkg.name %>.min.js'
      }
    }
  });

  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-uglify');

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

};

It is also possible to define custom tasks right inside the Gruntfile, in the grunt.register Task block, like the following one:

 grunt.registerTask('default', 'Log something', function() {
    grunt.log.write('I'm logging things.').ok();
  });

Conclusion

Of course, this is not a detailed guide, because Grunt is an extensive project (used, for example, by Twitter) with many plugins and a lot of options for per project customization.
But this simple configuration example shows how repetitive tasks can be automated, giving developers more time to work on improving their projects, implementing ideas, etc.

  • I profited 104,000 bucks in last 12 months by working from my home a­­n­­d I was able to do it by w­o­r­k­i­n­g in my own time f­o­r 3 or sometimes more hrs each day. I was following a money making opportunity I found online and I am happy that I was able to earn so much extra income. It’s beginner friendly and I’m just so blessed that I found out about it. This is what i did… EASYURL.NET/f2265

  • [email protected]

    I’ve made 104000 dollars last year by doing an on-line job at my house and I did that by wor­king part-time f­­o­­r 3+ hours each day. I was following work model I found on-line and I am excited that i earned so much money. It’s user-friendly and I’m just so grateful that i found this. Here’s what I did… EASYURL.NET/9861f

  • [email protected]

    I’ve made 104,000 bucks in 2016 by doing an online job from home and I did that by working part-time f­­o­­r 3+ hrs each day. I used a business opportunity I found on-line and I am so amazed that i made so much money. It’s so beginner friendly a­n­d I’m so grateful that I found out about it. Check out what I do… http://jump­.­wtf/CCcHHu1

  • [email protected]

    I profited $104,000 last year by freelancing from home a­­n­­d I did it by w­orking part time for 3 or sometimes more hrs each day. I used work opportunity I found online and I am so thrilled that i earned so much extra income. It’s very newbie-friendly a­n­d I’m so grateful that i learned about it. Here is what i do… http://itreplaceitall­.­com

  • [email protected]

    I got paid 104,000 thousand dollars previous year by working from my house a­n­d I did it by working part-time f­o­r 3+ hrs daily. I followed a money making opportunity I stumbled upon online and I am amazed that i made so much money on the side. It’s so beginner friendly a­n­d I’m just so blessed that i discovered it. Check out what I did… http://budget25­.­weebly­.­com

  • [email protected]

    I have made 104000 dollars in last 12 months by freelancing on-line and I did that by working part-time for several h /daily. I used a business opportunity I was introduced by this web-site i found on-line and I am happy that i made so much money. It’s very newbie-friendly a­­n­­d I’m just so grateful that I found out about this. Check out what I do… http://tny­.­im/8Ah

  • I have profited 104000 bucks last year by working online from my house a­­n­­d I did that by w­orking part-time for few h /daily. I followed a business opportunity I came across from company that i found online and I am so thrilled that I was able to earn so much extra income. It’s very user-friendly a­n­d I am just so grateful that i found this. Check out what I do… http://gee­.­su/RexYV

  • I profited 104 thousand bucks previous year by doing an online job at my house a­n­d I did it by w­o­r­k­i­n­g part time f­­o­­r 3 or sometimes more hrs every day. I was following an earning opportunity I found online and I am excited that I was able to earn so much extra income. It’s really newbie-friendly a­n­d I’m so happy that I found out about it. Check out what I did… http://www.wzurl­.­me/tEXzrw

  • alice.masterson

    I got paid 104000 bucks in 2016 by doing an online job and I did it by w­orking part time for 3+ hours each day. I followed a business opportunity I stumbled upon online and I am so thrilled that i earned so much extra income. It’s user friendly a­n­d I am just so grateful that I found out about it. This is what i did… http://www.wzurl­.­me/m7IQvg

  • bell_raymond

    I got paid $104,000 last year by doing an on-line job a­n­d I did that by w­orking in my own time f­o­r 3 or sometimes more hrs every day. I was following a business model I came across from company that i found online and I am thrilled that i made so much money. It’s so newbie friendly and I’m so happy that I found out about this. Check out what I do… http://urlof­.­site/Ag1NW

  • [email protected]

    I was paid 104000 dollars last year by doing a web based work and also I was able to do it by w­orking in my own time f­o­r several hours everyday. I utilized job opportunity I found out on the net and so I am delighted that I was able to earn such great money. It’s seriously newbie-friendly and I am so grateful that I discovered out regarding it. Take a look at what I do… I was without work for six months time when my early Fellow worker lastly strongly suggested me to get into freelancing online… It was really after I received $5000 in my initial month when I seriously believed I really could do this for a living! Now a days I am joyful than ever… I work at home and I am my own boss now that I always wanted… I see so many unhappy people around me, working the same old boring job that’s sucking the life out of all of them day-after-day… Each time I notice someone like that I say START FREELANCING MAN! This is where I started out >>>> www­.­cat­.­org­.­uk/snip/78276

  • [email protected]

    I was paid 104000 bucks past 12 months by doing an internet job and I was able to do it by w­orking in my own time f­o­r several hours every day. I tried job opportunity I found over the internet and also I am thrilled that I was capable to make such decent cash. It’s seriously newbie-friendly and therefore I’m so pleased that I found out about it. Look out for exactly what I do… www­.­cat­.­org­.­uk/snip/93439

  • lawrence_lee

    I was paid 104000 dollars previous year by doing a web-based work while I was able to do it by w­orking in my own time f­o­r quite a few hours each day. I applied job opportunity I came across online and therefore I am excited that I was succeed to make such decent cash. It’s really newbie-friendly and therefore I am so delighted that I discovered out about it. Find out more about exactly what I do… http://polr­.­me/1g1h