Book Image

Getting Started with Grunt: The JavaScript Task Runner

By : Jaime Pillora, Bocoup LLC
Book Image

Getting Started with Grunt: The JavaScript Task Runner

By: Jaime Pillora, Bocoup LLC

Overview of this book

Table of Contents (12 chapters)


Getting Started with Grunt: The JavaScript Task Runner is an introduction to the popular JavaScript build tool, Grunt. This book aims to provide the reader with a practical skillset, which can be used to solve real-world problems. This book is example driven, so each feature covered in this book is explained and also reinforced through the use of runnable examples, this dual method of learning will provide the reader with the means to verify that the theory aligns with its practical use.

All of the software used in this book is open source and when covered, some will be accompanied with a short history while crediting the author. These open source developers do not release their work for monetary gain, instead, they hope to provide utility for others and to forward the community, and for this, they should be duly recognized.

What this book covers

Chapter 1, Introducing Grunt, explains exactly what Grunt is and why we would want to use it. Then, instead of starting at the very beginning, we temporarily jump ahead to review a set of real-world examples. This gives us a glimpse of what Grunt can do, which will help us to see how we could use Grunt in our current development workflow.

Chapter 2, Setting Up Grunt, after finishing our forward escapade, we jump back to the very beginning and start with the two primary technologies surrounding Grunt: Node.js and its package manager—npm. Then, we proceed to installing each of these tools and setting up our first Grunt environment. Next, we learn about the package.json and Gruntfile.js files and how they are used to configure a Grunt build. We will also cover the various Grunt methods used for configuration and the types of situations where each is useful.

Chapter 3, Using Grunt, extends on what we learned in the previous chapter, to the use and creation of tasks that consume our freshly made configuration. We will cover tasks, multitasks, and asynchronous tasks. We look in-depth into the task object and how we can use it effectively to perform common file-related actions. Finally, we review running Grunt tasks and methods that customize Grunt execution to our benefit.

Chapter 4, Grunt in Action, begins with an empty folder and gradually constructs a Grunt environment for a web application. Throughout this process, we use various examples from Chapter 1, Introducing Grunt, make use of the configuration strategies from Chapter 2, Setting Up Grunt, and include some extra features from Chapter 3, Using Grunt. At the end of this chapter, we shall be left with a Grunt environment that compiles and optimizes our CoffeeScript, Jade, and Stylus, and deploys our resulting web application to Amazon's S3.

Chapter 5, Advanced Grunt, introduces some of the more advanced use cases for Grunt; these introductions are intended to be purely an entry to each topic while providing the resources to learn more. We briefly cover testing with Grunt, Grunt plugins, advanced JavaScript, development tools and more.

What you need for this book

In order to run Grunt, you need an operating system capable of running Node.js; this includes Windows, Mac OS X, and certain flavors of Linux. You also need a command-line interface of some form; in Windows, you can use PowerShell or Command Prompt, and on Mac OS X and Linux, you will find a Terminal application available for use.

Who this book is for

The only requirement for this book is a basic understanding of JavaScript. The two most important JavaScript concepts to know are objects and functions. An understanding of how JavaScript Object Notation (JSON) data is structured is also required, however, this will follow naturally from learning JavaScript objects. From this starting point, you are able to enter the world of Grunt and begin to improve your development workflow.

If you are not familiar with JavaScript yet, Code Academy ( offers a fast and interactive introduction to the basics of JavaScript programming. If you have more time, Marijn Haverbeke's Eloquent JavaScript ( is a perfect book to give you a general understanding of what programming actually is, while focusing on JavaScript at the same time. In the free (Creative Commons License) HTML version of Eloquent JavaScript, Marijn uses the fact that you are reading the book in a Web Browser to his advantage by allowing you to run and edit the code examples right in the page. This interactive reading experience is extremely powerful, and I highly recommend Eloquent JavaScript.


In this book, various font styles are used to differentiate between different types of information. Here are some examples of these styles, and an explanation of their meaning:

When referring to a short piece of information that relates to the code examples, like a variable or property name, or file or directory name, we'll use a light mono-space font:

"Based on this task, we notice that each file in the files array contains src and dest properties."

When referring to a large piece of information that relates to the code examples, like a portion of code, the contents of a file or the output from the command-line interface, we use black mono-space font:

  stringCheck: {
    file: './src/app.js',
    string: 'console.log('

When referring to portion code in code, we note the example number and name at the top in a JavaScript comment (that is, text beginning with //).

When specifying user command line input among the command-line output, using Unix bash convention, we prepend a dollar symbol so we know what is input and what is output:

$ echo "hello world"
hello world

When referring to new terms and important words, we display them in bold.

When conveying a URL, we'll prefix the text with "http://" and use a mono-space font. This book's homepage ( is used as a URL shortener and as an intermediary in case URLs need to be updated. For example:

"For more information, see the Grunt Website at"

Code examples

You can download the code examples for Getting Started with Grunt at This URL will bring you to the Git repository housing the examples for this book. Here, you can find the instructions for downloading and running these examples. Once downloaded, you will find five folders, one for each chapter. Throughout this book, many code snippets begin with a JavaScript comment, referencing where that portion of code can be found within Code examples. For example, in Chapter 2, Setting Up Grunt, the first code snippet begins with //Code example 01-modules. Since we are currently reading Chapter 2, Setting Up Grunt, you will find the 01-modules example inside the gswg-examples/2/01-modules folder. If you are having problems running any of the examples or if you find a bug in any of the examples, please open an issue on Github here:

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic in which you have expertise and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.


Though we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.