For a project to make use of the Grunt framework, it will require the installation of its libraries and the setting up a bare minimum configuration file. The libraries provide the framework and tools required by all Grunt plugins, and the configuration file provides a starting point from which we can start loading plugins and adjusting their behavior.
It's usually a good idea for a project to be packaged in a way to help keep track of dependencies, binaries, scripts, maintainers, and other important information. The standard package format for Node.js-based projects is CommonJS.
At the heart of the CommonJS package, lies the package.json
file. This file contains everything important about the package and is stored in the JSON format. The simplest way to create a package.json
file is to use the npm init
command. This command will ask a series of questions and generate a package.json
file based on the answers provided. Here's an example of the questions that are asked when you run the command:
name: (grunt-book) myproject version: (0.0.0) description: My first Grunt project. entry point: (index.js) test command: git repository: keywords: author: license: (ISC)
After these questions are answered, a package.json
file will be generated in the current path with the following contents:
{ "name": "myproject", "version": "0.0.0", "description": "My first Grunt project.", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
The following steps take us through installing the Grunt framework libraries on our project and creating a bare minimum configuration file.
First, we'll install the Grunt libraries in the current project path, and have it added to our project dependencies. This can all be done by using the following command:
$ npm install --save grunt
Tip
Due to our use of the
--save
flag with theinstall
command, the Grunt package will be added to the dependency list of the project's package. This can be confirmed by taking a look at thedependencies
property inside thepackage.json
file.The
--save-dev
flag is also available for use with theinstall
command when you'd like the installed packages to be added to thedevDependencies
property, which lists the dependencies to set up a development environment.Next, we'll set up an empty configuration file that would, at the very least, allow Grunt to run and also provides a place for future task configurations. Let's create a file called
Gruntfile.js
in the root directory of our project with the following contents:module.exports = function (grunt) { grunt.initConfig({}); grunt.registerTask('default', []); };
Now that we have the Grunt libraries installed and a basic configuration file set up, we can use the
grunt
command to test that it's all working as expected. Running thegrunt
command in the terminal should now produce output similar to the following:Done, without errors.
When the Grunt CLI tool is used, it always looks for the nearest file named Gruntfile.js
, from which it then attempts to load configurations. Inside the configuration file, there is an exported function that receives one argument. This argument is an object that provides us with access to the Grunt framework to load, create, and configure tasks.
At this point, we have no tasks loaded or created, and no configurations defined. Our default
task is also set to do nothing, so running the grunt
command did nothing except report that it was successfully completed.