GruntJS is a JavaScript task runner written on top of NodeJS. It can be used to automate repetitive tasks for your application like minification, compilation, unit testing, linting and more; with minimum effort.
Prerequisites
Before you begin, you should have a non-root user account with sudo
privileges set up on your system.
Installation
Refresh your local package index:
sudo apt-get update
Install curl
:
sudo apt-get install curl
Add the package repository of NodeJS 8.x with the following command:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
Next, we need to install NodeJS and NPM all together along with the development tools:
sudo apt-get install build-essential nodejs
Now check to ensure NodeJS and NPM are working:
node --version && npm --version
#v8.x.x
#5.x.x
Install Grunt:
sudo npm install -g grunt-cli
This will install Grunt globally on your system. Run the following command to check the version installed on your system:
grunt --version
#grunt-cli v1.2.0
Install Grunt on a new project
To install grunt on a new project we will need to add two files to your project directory: package.json
and Gruntfile.js
.
-
package.json
: This file is used by NPM to store meta-data for projects published as NPM modules. -
Gruntfile.js
: This file is named
Gruntfile.jsor
Gruntfile.coffee` and is used to configure or define tasks, as well as to load Grunt plugins.
Navigate to the root directory of your package:
cd /path/to/project
Run the following command to create a package.json
file:
sudo npm init
Answer the questions in the command line questionnaire.
Once your package.json
file is created, install Grunt as a development dependency:
sudo npm install grunt --save-dev
Create the Gruntfile.js
file:
nano Gruntfile.js
Register a simple default task:
var grunt = require('grunt');
grunt.registerTask('default', 'default task description', function(){
console.log('hello world');
});
Now, run the default task:
grunt
That concludes our tutorial, thank you for reading.
Want to contribute?
You could earn up to $300 by adding new articles
Suggest an update
Request an article