Setting up Grunt from scratch

08 Sep 2013

Being a modern web designer involves a lot of tools. Grunt is currently my new hotness. Its a tool I use to manage a bunch of other tools I use. In fact its consolidated about 5 different tools I had into one click (well command but more on that later).

In this post I'll walk through getting set up with Grunt and in the next post, we'll set up Grunt to handle Sass with Compass, Jekyll, JS concatenation and minification, Live reloading changes to any of the HTML/CSS/JS and finally multi-device browsing and browser refresh. Boom.

Rule 1: Don’t fear the Terminal

We’re gonna get in the command line in a minute. I promise it isn’t gonna get weird. I used to hate it too. Then I got to love it. Then I started using it for loads of stuff. Then I hated it, because all the tabs or windows I needed open drove me nuts. What’s so beautiful about Grunt, is you can run loads of commands in one line. Stick with me on this, its awesome1.

Just follow along with what I’m doing by copying everything out of the code example after the $ sign (that’s just t omake it look more like OSX terminal and makes me feel cool). The commands examples are the same for Mac and Windows and if they aren’t I’ll write them both out cos I got your back.

Windows note: You can’t ctrl+v to paste in CMD in windows. You can right-click and paste though.

Install all the things

Ok So first we gonna need to get our Node on. This is a cinch. Go to the Node JS Website. Click install. Job done. You are one of the cool kids.

When you install node, you also get NPM. NPM is a big library of Node modules (kinda like apps or plugins built with and for Node). Its also how we’re gonna install Grunt. Its Terminal time.

Open up the Terminal (Mac) or Cmd (Windows) and type in:

1

$ npm install -g grunt-cli

Lets break that down:

“npm” tells terminal/cmd what program we’re commanding

“install” is the command

“-g” is an argument (a kind of optional bit of info) for the command short way of saying “–global” This means we’re installing Grunt globally on our system. This is a good thing. You want to be able to use it everywhere.

“grunt-cli” Is another argument. This time telling NPM what you want to install. In this instance the Grunt Command Line Interface.

The terminal or the cmd window is gonna go a bit Matrix for a bit. It’s basically telling you what its doing. It can look a bit scary, but should all be fine.

When I installed something from NPM for the first time on a mac, I got an EACCESS Error (which scared me). Didn’t happen on Windows though.

A lot of solutions online tell you to just install the package again like this, using SUDO.

1

$ sudo npm install -g grunt-cli

Ummm. Don’t do that. Although I trust Bocoup and the community around the grunt-cli, this is a bad habit to get into. The sudo command is basically the airport security for your mac waiving someone through an airport security check. Sometimes thats good. Sometimes we want people to be checked.

Getting ready to Grunt

First we’re going to get the terminal/command prompt to the right folder. To do this we use the change directory command or cd.

1

$ cd path/to/my/project-folder

and hit enter. The terminal should show you what folder you’re now in. This means any commands you run now will be specific to that folder.

Mac note: on a mac, you can do this

1

$ cd ~/path/to/project

The ~ puts you in your home directory.

Windows note: on windows, I do this

1

>_ cd c:\path\to\project

Setting up a Grunt on a new or existing project is easy. You just need to add 2 files to the folder where everything lives: Gruntfile.js and package.json. These files are pretty central to how Grunt works. The package.json file tells node all the things you need to include in your project and also some general information, which might be useful if your project is on github or something. Here’s an example fo the package.json file for this site, with some dependecies removed so its not really long:

Recap

Ok. That was more epic than I intended but the initial set up is the hardest/least fun bit. We installed Node.js and NPM. Got used to using the command line, installed Grunt globally and set up our project to use Grunt. You should feel like a fucking boss. Good job. Next time all the cool stuff.