Boris Burkov
Boris Burkov

Reputation: 14506

`Fatal error: Unable to find local grunt.`: why asking for local grunt, if I have a global?

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?

Upvotes: 2

Views: 12532

Answers (4)

Abhishek Kumar
Abhishek Kumar

Reputation: 1040

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.

Upvotes: 0

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123563

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.

Upvotes: 9

Brandon Horst
Brandon Horst

Reputation: 2070

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

npm install -g grunt-cli
npm install grunt

Upvotes: 0

Mike Quinlan
Mike Quinlan

Reputation: 2882

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).

Upvotes: 1

Related Questions