2

I'm a noob in Node.js and trying to use grunt-contrib-concat to build my javascript library from multiple source files.

I've created a virtual environment, activated it, globally installed grunt and grunt-cli, written Gruntfile.js and package.json files for my project. To summarize:

$ nodenv NODEENV
$ source NODENV/bin/activate
(NODEENV)$ npm install -g grunt-cli
...
(NODEENV)$ npm install -g grunt
...
(NODEENV)$ cd myproject
(NODEENV)$ ls myproject
Gruntfile.js package.json src test README.md
(NODEENV)$ grunt test
grunt-cli: The grunt command line interface. (v0.1.13)

Fatal error: Unable to find local grunt.

If you're seeing this message, either a Gruntfile wasn't found or grunt
hasn't been installed locally to your project. For more information about
installing and configuring grunt, please see the Getting Started guide:

http://gruntjs.com/getting-started

What I don't understand is why grunt-cli is telling me that it wants a local per-project version of grunt if I've already got a globally installed one?

Could you also suggest a way to fix the error?

Boris Burkov
  • 13,420
  • 17
  • 74
  • 109
  • Did you try `npm install` from your project directory? I'm a Grunt noob, but I'm fairly certain it has to reside in your project's `node_modules` directory to assert your project is using the correct version of grunt. – Mike Quinlan Jun 04 '14 at 15:05
  • @Mike: thanks. Indeed, it worked. I wonder, if installing Grub globally is pointless? – Boris Burkov Jun 04 '14 at 15:10
  • Possible duplicate: http://stackoverflow.com/a/27115342/687677 – superluminary Jan 05 '15 at 13:12

4 Answers4

9

It's due to npm's foldering:

  • Install it locally if you're going to require() it.
  • Install it globally if you're going to run it on the command line.

The Gruntfile itself depends on require('grunt') being available, requiring a local install.

$ npm install grunt

And, having grunt(1) as an available command makes using the tool easier, requiring a global install.

$ npm install -g grunt-cli

To have both, though, it has to be installed in both folders.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
1

Run npm install from your project directory. For each project, Grunt needs to reside in the npm_modules directory of your project in order for the project to assert that it is using the correct version of grunt (which may differ from your global version).

Mike Quinlan
  • 2,873
  • 17
  • 25
0

That's just the way it works. You always need to:

npm install -g grunt-cli
npm install grunt
Brandon Horst
  • 1,921
  • 16
  • 26
0

If these commands does not work with/without sudo.

npm install -g grunt-cli
npm install grunt

or you are getting error like this

Error 1 Error 2

Then follow these steps.

  1. Run npm config list then check for registry key value. it can be like:

    registry = "https://npm.some.domain.other.than.global.com/"

  2. if you found your case as step 1 then run command

    npm config set registry https://registry.npmjs.com/

Done!!

Now you can run npm install if your package.json contains grunt library name,

otherwise

npm install -g grunt-cli
npm install grunt

It will work now.

Abhishek Kumar
  • 820
  • 10
  • 18