16

I'm in the process of reworking my build system, and I've read that node.js with grunt is a good way to go. I've never used either, and I'm having a bit of trouble.

The problem is that I want to set up a portable build environment that I can include in the version control for my project (maybe this isn't possible). Getting node.js and npm working has been no trouble. But, every instruction I see for installing grunt says to use the -g flag with npm which installs it globally. Since I want a totally portable environment, I have attempted to leave this off, but I can't get grunt to work.

Am I missing something, or is what I'm attempting to do not feasible?

Dominic P
  • 2,284
  • 2
  • 27
  • 46
  • I wound up using ant instead for this project. I would have liked to get grunt working, but I couldn't afford any more time on the project, and I was able to get ant up and running much faster in my environment. – Dominic P Apr 03 '13 at 18:30

4 Answers4

22

Have a look at http://gruntjs.com/getting-started

Grunt has recently been split into a project-local dependency (grunt) and a command-line launcher (grunt-cli). It's the latter that should be installed globally.

As an extra hint on ensuring that you can take your builds everywhere: make sure you save all dependencies in package.json, by using the --save and --save-dev parameters when using npm install. More info: https://npmjs.org/doc/install.html

Ruben Vermeersch
  • 1,933
  • 1
  • 18
  • 27
  • Thanks for the helpful info. I did take a look at the docs you referenced. I noticed that they still required `grunt-cli` to be installed globally. Is there any way around that? – Dominic P Apr 03 '13 at 06:29
  • There's little need to work around that. All `grunt-cli` does is launch the grunt version that's embedded in your project. Consider it a convention. Depending on it won't make your project less portable. – Ruben Vermeersch Apr 04 '13 at 07:11
  • 2
    You can install grunt-cli locally and use an npm script to kickoff the workflow. See the bottom of the page: https://npmjs.org/package/grunt-cli – M69 Jan 27 '14 at 19:06
19

You can use local grunt without global (-g) installation of a grunt-cli by calling:

node node_modules/grunt-cli/bin/grunt --version

Of course first you need install it in you project locally and have a grunt version greater than 0.3; for example:

npm install grunt-cli
npm install grunt@0.4.5

Or add them both to your packages.json and call

npm install  

This should also help when you just can't install any package globally as I've described in https://stackoverflow.com/a/39046355/2201879

Community
  • 1
  • 1
tomajar
  • 419
  • 5
  • 7
2

See https://www.npmjs.com/package/grunt-cli#installing-grunt-cli-locally:

Installing grunt-cli locally

If you prefer the idiomatic Node.js method to get started with a project (npm install && npm test) then install grunt-cli locally with npm install grunt-cli --save-dev. Then add a script to your package.json to run the associated grunt command: "scripts": { "test": "grunt test" }. Now npm test will use the locally installed ./node_modules/.bin/grunt executable to run your Grunt commands.

To read more about npm scripts, please visit the npm docs:
https://docs.npmjs.com/misc/scripts.

splintor
  • 9,924
  • 6
  • 74
  • 89
1

Here is the command line code that will install the latest version of Grunt in your project folder, adding it to your devDependencies:

npm install grunt --save-dev

The same can be done for gruntplugins and other node modules. As seen in the following example installing the JSHint task module:

npm install grunt-contrib-jshint --save-dev

Checkout the current available gruntplugins to be installed and used on your project at the plugins page.

Be sure to commit the updated package.json file with your project when you're done!