Using Grunt

done the right way

Gergely Nemeth
Javascript and the server

--

All my latest articles can be found at the RisingStack blog.

Installing node modules globally is bad. You should always use your project specific one — this way you can have multiple versions in your system at the same time. This is very useful when you want to try out the beta versions of Grunt or your projects require different Grunt versions.

The trick that makes it possibly to avoid globally installed grunt or grunt-cli is that npm will automatically set up $PATH to look in node_modules/.bin.

To generate the skeleton of your package.json file, you can use:

npm init

This produces the following lines:

{
"name": "my-project",
"version": "0.0.0",
"description": "grunt without global install",
"main": "app.js",
"dependencies": {},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "w"
},
"license": "ISC"
}

Of course, the scripts section can be extended with custom scripts. We will use this to run our grunt tasks.

“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1",
"grunt": "grunt"
}

The last thing you need is to install grunt locally:

npm install grunt --save-dev

That’s it, you can run your local version of grunt without the grunt-cli with this:

npm run grunt

(Tip by @oroce: you can define an `npm run deploy` script which runs `grunt deploy` — this enables you to hide your build system and easily switch it later on)

Further readings:

--

--

Gergely Nemeth
Javascript and the server

Engineering Manager | Built @RisingStack, @GodaddyOSS @BaseWebReact | Find me at https://nemethgergely.com