"Fatal error: Unable to find local grunt." when running "grunt" command - node.js

I uninstalled grunt with following command.
npm uninstall -g grunt
Then I again installed grunt with following command.
npm install -g grunt-cli
Visit following link:
https://npmjs.org/package/grunt-html
I want to use the above grunt plugin
But when I run the grunt command it gives me following error:
D:\nodeJS\node_modules\grunt-html>grunt
grunt-cli: The grunt command line interface. (v0.1.6)
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

All is explained quite nicely on gruntjs.com.
Note that installing grunt-cli does not install the grunt task runner!
The job of the grunt CLI is simple: run the version of grunt which has
been installed next to a Gruntfile. This allows multiple versions of
grunt to be installed on the same machine simultaneously.
So in your project folder, you will need to install (preferably) the latest grunt version:
npm install grunt --save-dev
Option --save-dev will add grunt as a dev-dependency to your package.json. This makes it easy to reinstall dependencies.

You have to install grunt in your project folder
create your package.json
$ npm init
install grunt for this project, this will be installed under node_modules/. --save-dev will add this module to devDependency in your package.json
$ npm install grunt --save-dev
then create gruntfile.js and run
$ grunt

I think you have to add grunt to your package.json file. See this link.

I had this issue on my Windows grunt because I installed the 32 bit version of Node on a 64 bit Windows OS. When I installed the 64bit version specifically, it started working.

I had the same issue today on windows 32 bit,with node 0.10.25, and grunt 0.4.5.
I followed dongho's answer, with just few extra steps.
here are the steps I used to solve the error:
1) create your package.json
$ npm init
2) install grunt for this project, this will be installed under node_modules/. --save-dev will add this module to devDependency in your package.json
$ npm install grunt --save-dev
3) then create gruntfile.js , with a sample code like this:
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
options: {
globals: {
jQuery: true
}
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['jshint']
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['jshint']);
};
here, src/**/*.js and test/**/*.js should be the paths to actual JS files you are using in your project
4) run npm install grunt-contrib-jshint --save-dev
5) run npm install grunt-contrib-watch --save-dev
6) run $ grunt
Note: when you require common package like concat, uglify etc, you need to add those modules via npm install, just the way we installed jshint and watch in step 4 & 5

if you are a exists project, maybe should execute npm install.
guntjs getting started step 2.

This solved the issue for me. I mistakenly installed grunt using:
sudo npm install -g grunt --save-dev
and then ran the following command in the project folder:
npm install
This resulted in the error seen by the author of the question.
I then uninstalled grunt using:
sudo npm uninstall -g grunt
Deleted the node_modules folder. And reinstalled grunt using:
npm install grunt --save-dev
and running the following in the project folder:
npm install
For some odd reason when you global install grunt using -g and then uninstall it, the node_modules folder holds on to something that prevents grunt from being installed locally to the project folder.

Related

All the npm package not installed globally can't be executed directly

Symptom:
I can't execute the npm packages directly that are not installed globally. But I can execute it by npm scripts. How to fix it?
For example:
I installed gulp under the project:
npm install gulp --save-dev
Then I try to execute it by
gulp
zsh: command not found: gulp
But if I add a npm script to package.json:
"scripts": {
"test": "mocha --require intelli-espower-loader && gulp test",
"start": "gulp"
},
Then run
npm start
It can get executed without problem.
P.S.
Same issue with the mocha package, I can't execute mocha directly but I can execute npm test without problem.
Help Wanted:
What I can do to fix that issue?
Notice:
I'm not saying that I want to execute them globally, I just want to execute them under the project.
I don't know where goes wrong, but they are executable not long ago, just don't work recently!
In addition to #Ion's answer: You might need to add the path to the environment variables. In windows OS, it would be %AppData%\npm. For packages installed locally, you should be able to run them like
.\node_modules\.bin\gulp
If you want to execute them globally install with the -g flag
npm install gulp -g
To run directly you could also do ./node_modules/.bin/gulp
That is because the package not installed globally doesn't create a reference in the root node_modules folder . If you want to specifically use the local installed version to run globally on terminal then go to you environment variables and set the path to ./node_modules/gulp/bin/gulp.js or alternatively configure your npm to use the mentioned location as the gulp and execute node_modules/.bin/gulp
You have two choices:
Install the executable modules globally:
npm install -g gulp
This is good if it's a tool you use often (for many different projects). For gulp specifically, the globally installed gulp will also check to see if the project has its own gulp installed (in node_modules) and run that version of gulp. This means that each project can have its own version of gulp. But not all tools have this feature.
Use npx:
npx gulp
Newer versions of npm ships with a command called npx which will search through your project's node_modules directory to find a module to execute. This means you don't need to install project specific tools globally. Apart form avoiding global installs of commands (which may or may not be a security issue) it also allows you to install project specific versions of tools.
If your npm installation does not have npx you can install it from npm itself:
npm install -g npx

Where to install gulp?

I'm trying to leverage from Elixer so I started to get up and running. I've installed Node and Npm. Later I also installed Gulp using sudo npm install gulp -g. But this installed Gulp in my project directory, resulting in a massive file transfer from my local host to the webserver. Is it necessary to install Gulp inside the project directory? Or is there a better way to install it somewhere else and use it for any project needed?
Sorry if this is a total beginners question but I can't seem to find an answer online. Thanks.
There is only few steps to start use it.
Install globally $ npm install --global gulp
Install directly to your project root dir $ npm install --save-dev gulp
Create gulpfile.js in root dir
Then in your new gulpfile do the follow:
var gulp = require('gulp');
gulp.task('default', function() {
// place code for your default task here
});
and after that just type gulp command in terminal to execute default task
here is documentation to help you started with gulp docs and here you can find packages to use it npmjs.com
Tip: if you on OSX use sudo to install npm/jspm/gulp etc
sudo npm install gulp -g shouldn't be installing gulp in your project directory. However, Gulp does always need to be installed locally in the project folder in order for it to work. Otherwise, you will get an error when trying to run Gulp. The -g global installion of Gulp is only needed for linking the shell to the binary $ gulp; it will dispatch to the local gulp program as soon as it is called.
Gulp, bower, ... are dependencies.
The command npm install ... will download the module to the directory named node_modules. They need to be (litterally) a part of your project. Think of it as a pure JS library (as it actually is).

Installing Grunt, osx, nvm, zsh

Installing Grunt
I have osx yosemite, zsh, nvm. The version of node I am using is v0.10.33.
I installed node in a way I do not need to use sudo (node and npm reside in my home folder)
here where I export the global dependencies in my zshrc
export NODE_PATH=/Users/nickname/.node/lib/node_modules/:/Users/nickname/.node/lib/node_modules
Just for information, I am trying to install Ghost (the blogging plataform).
I cloned like this to get the stable version
git clone -b stable https://github.com/TryGhost/Ghost.git ghost
and then
cd ghost/
git submodule update --init
npm install -g grunt-cli
npm install
grunt init
The error I am getting is that is not able to find grunt
Ghost git:(stable) grunt init
zsh: command not found: grunt
You're not supposed to install grunt globally, only grunt-cli. The grunt package should be installed in your project directory using something like:
~$ npm install grunt --save-dev
That will add a line to your project's package.json file, then you configure a "Gruntfile.js" for your project with tasks that are run from the command line with:
~$ grunt taskname

Grunt installation in mac os

I want to create angular js app using bootstrap, for that I installed node.js pkg file in my mac.
Afterwards I installed yeoman ($sudo npm install --global yo). Documentation says if we install yo then we get grunt and bower automatically installed. In my case grunt and bower are not being installed.
So I decided to install it externally. I used following commands:
$sudo npm install -g bower => it works perfectly and giving me all files and packages.
$ sudo npm install -g grunt => it works perfectly but not giving me all files and packages
grunt are installed on path of usr/local/lib/node_modules/grunt
grunt folder contains files
constributing.md
licenses-mit
readme
aaveyor.yml
internal-tasks
lib
node_modules
package.json
Which files I am missing?
If I run command $ grunt --version then I got following error
-bash: /grunt: No such file or directory
How to resolve this?
You have to run:
$ npm install -g grunt-cli
The global binary command-line tool is grunt-cli, not grunt. It's a little bit confusing in the beginning, as grunt-cli provides a global binary which is callable by grunt.
So, in the end:
Install grunt locally in your application.
Install grunt-cli globally.
Then, you can run grunt from anywhere as expected :-)

grunt init:node not working

I'm trying to do an intro grunt tutorial. I've installed git, node.js, and grunt globally (or at least I thought I did using: npm install -g grunt (which installs). I then made a quick directory and entered it (mkdir demo, cd demo), but when I type:
> grunt init:node
at the prompt I get the following:
grunt-cli: The grunt command line interface (v0.1.11)
Fatal error: unable to find local grunt
If you're seeing this message, either a Gruntfile wasn't found or 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
Which I've looked at and it says to do what I'm doing and what the tutorial mentions??? Any ideas what I've done wrong? Node.js and Git have been working fine so I can only assume it is grunt or the install that has failed.
Thanks
with grunt you need two elements. a global grunt-cli as you have installed using npm install -g grunt-cli and also a local (to the project) copy of grunt itself. so in the folder of your project install this with npm install grunt --saveDev this will install a local grunt and also add it to your devDependencies in your package.json file. you will also need a Gruntfile.js in the project folder. This is a great write-up http://www.integralist.co.uk/Grunt-Boilerplate.html

Resources