Search code examples
javascriptnode.jsgruntjs

`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?


Solution

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