Book Image

Mastering Grunt

By : Daniel Li
Book Image

Mastering Grunt

By: Daniel Li

Overview of this book

<p>Grunt.js continues to excel as the build automation tool of choice. Along with its support for many third-party technologies, Grunt is packaged with a clean API for defining tasks. This powerful tool can streamline your workflow by automating the preparation tasks for production, such as compression, compilation, obfuscation, testing, and even pushing your web application live. This book will teach you how to master build automation and testing with Grunt. You will have the opportunity to utilize the latest and in-demand web tools, such as Git, Jade, CoffeeScript, Sass, and the Mocha testing engine, across several exciting projects by combining Grunt with them. You will also learn to create a project – a simple Bulletin Board System (BBS), which will explain the use of Grunt alongside the Mocha testing library to automate testing throughout the build process.</p> <p>Mastering Grunt will demonstrate how to leverage Grunt with other technologies to become an expert in build automation, teaching you the best practices for modern web development along the way.</p>
Table of Contents (12 chapters)

Introducing npm


npm is a package manager for the Node.js software platform. A package management system helps to streamline the installation, updating, and configuration of software packages. This way, one can easily manage the dependencies of a project, without any worries, in a scriptable manner.

The following is a look into the official website for npm:

As Grunt is a JavaScript project built using Node, it naturally uses npm for version control. npm will also be used to install and update Grunt plugins and Bower. The npm registry also hosts a variety of other tools, including the Mocha testing engine, the Jade templating engine, and the ExpressJS web framework.

Why use npm?

Traditionally, if someone wanted to install a package that was built using Node.js, one would need to download the necessary files and build the package manually. Using npm, one can install and update software packages with a simple command. It also allows the installation of projects of a specific version, a technique that will be used throughout this book to ensure that project instructions align consistently with the software.

Installing npm

Since npm comes with Node, it suffices to install the Node platform to set up npm.

Installing npm on Windows

The following steps will cover the installation of npm on Windows through the Node installer:

  1. Download the .msi installer located at http://nodejs.org/download/.

  2. Run the .msi installer to install Node and npm.

Installing npm on Mac OS X and Linux

The following steps will cover the installation of npm via the command line on Mac OS X and Linux:

  1. Download the .pkg installer located at http://nodejs.org/download/.

  2. Run the .pkg installer to install Node and npm.

  3. Optionally, if you are using Linux, determine your Linux distribution.

  4. Once you have found this, install Node for your specific distribution or Mac OS X, by following the instructions mentioned in the link https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager.

Once Node is installed, you can test npm by calling the following binary:

npm

You should see a trace similar to the following:

Usage: npm <command>

where <command> is one of:
    add-user, adduser, apihelp, author, bin, bugs, c, cache,
    completion, config, ddp, dedupe, deprecate, docs, edit,
    explore, faq, find, find-dupes, get, help, help-search,
    home, i, info, init, install, isntall, issues, la, link,
    list, ll, ln, login, ls, outdated, owner, pack, prefix,
    prune, publish, r, rb, rebuild, remove, repo, restart, rm,
    root, run-script, s, se, search, set, show, shrinkwrap,
    star, stars, start, stop, submodule, tag, test, tst, un,
    uninstall, unlink, unpublish, unstar, up, update, v,
    version, view, whoami

npm <cmd> -h     quick help on <cmd>
npm -l           display full usage info
npm faq          commonly asked questions
npm help <term>  search for help on <term>
npm help npm     involved overview

Using npm

To install a package in your current directory, you'll simply need to issue the following command:

npm install <package>

For instance:

npm install grunt

Note that at certain times, you may be interested in installing a package globally. To do this, you'll need to pass the -g flag, as shown in the following command:

npm install -g <package>

You may be interested in the Node packages installed in your current directory. You can list them using the following command:

npm ls

To update all the packages in your current directory, issue a simple update command. This is shown as follows:

npm update