node.js “致命错误:无法找到本地咕噜声。” 运行“grunt”命令时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15483735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:54:11  来源:igfitidea点击:

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

node.jsgruntjs

提问by Ashwin Hegde

I uninstalled grunt with following command.

我使用以下命令卸载了 grunt。

npm uninstall -g grunt

Then I again installed grunt with following command.

然后我再次使用以下命令安装了 grunt。

npm install -g grunt-cli

Visit following link: https://npmjs.org/package/grunt-html

访问以下链接:https: //npmjs.org/package/grunt-html

I want to use the above grunt plugin

我想使用上面的 grunt 插件

But when I run the grunt command it gives me following error:

但是当我运行 grunt 命令时,它给了我以下错误:

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

回答by asgoth

All is explained quite nicely on gruntjs.com.

所有在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.

请注意,安装 grunt-cli 不会安装 grunt 任务运行器!grunt CLI 的工作很简单:运行安装在 Gruntfile 旁边的 grunt 版本。这允许在同一台机器上同时安装多个版本的 grunt。

So in your project folder, you will need to install (preferably) the latest grunt version:

因此,在您的项目文件夹中,您需要安装(最好)最新的 grunt 版本

npm install grunt --save-dev

Option --save-devwill add gruntas a dev-dependencyto your package.json. This makes it easy to reinstall dependencies.

Option--save-devgrunt作为dev-dependency添加到您的package.json。这使得重新安装依赖项变得容易。

回答by Dongho Yoo

You have to install grunt in your project folder

你必须在你的项目文件夹中安装 grunt

  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 and run

    $ grunt 
    
  1. 创建你的 package.json

    $ npm init
    
  2. 为这个项目安装 grunt,这将安装在node_modules/. --save-dev 会将此模块添加到 package.json 中的 devDependency

    $ npm install grunt --save-dev
    
  3. 然后创建 gruntfile.js 并运行

    $ grunt 
    

回答by mpang

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

我认为您必须将 grunt 添加到您的package.json文件中。请参阅此链接

回答by mrichter

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.

我在 Windows grunt 上遇到了这个问题,因为我在 64 位 Windows 操作系统上安装了 32 位版本的 Node。当我专门安装 64 位版本时,它开始工作。

回答by Naeem Shaikh

I had the same issue today on windows 32 bit,with node 0.10.25, and grunt 0.4.5.

我今天在 Windows 32 位上遇到了同样的问题,节点为 0.10.25,grunt 为 0.4.5。

I followed dongho's answer, with just few extra steps. here are the steps I used to solve the error:

我跟着dongho 的回答,只需几个额外的步骤。这是我用来解决错误的步骤:

1) create your package.json

1) 创建你的 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

2) 为这个项目安装 grunt,这将安装在 node_modules/ 下。--save-dev 会将此模块添加到 package.json 中的 devDependency

$ npm install grunt --save-dev

3) then create gruntfile.js, with a sample code like this:

3) 然后gruntfile.js使用如下示例代码创建:

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/**/*.jsand test/**/*.jsshould be the paths to actual JS files you are using in your project

在这里,src/**/*.js并且test/**/*.js应该是你正在使用你的项目的路径,以实际JS文件

4) run npm install grunt-contrib-jshint --save-dev

4)运行 npm install grunt-contrib-jshint --save-dev

5) run npm install grunt-contrib-watch --save-dev

5)运行 npm install grunt-contrib-watch --save-dev

6) run $ grunt

6)运行 $ 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

注意:当你需要像 concat、uglify 等通用包时,你需要通过添加这些模块npm install,就像我们在步骤 4 和 5 中安装 jshint 和 watch 一样

回答by lingyfh

if you are a exists project, maybe should execute npm install.

如果你是一个存在的项目,也许应该执行 npm install。

guntjs getting startedstep 2.

guntjs 入门步骤 2。

回答by fourfridays

This solved the issue for me. I mistakenly installed grunt using:

这为我解决了这个问题。我错误地使用以下命令安装了 grunt:

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:

这导致了问题作者看到的错误。然后我使用以下命令卸载了 grunt:

sudo npm uninstall -g grunt

Deleted the node_modules folder. And reinstalled grunt using:

删除了 node_modules 文件夹。并使用以下命令重新安装 grunt:

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.

出于某种奇怪的原因,当您使用 -g 全局安装 grunt 然后将其卸载时,node_modules 文件夹会保留一些阻止 grunt 本地安装到项目文件夹的内容。