73

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

npm uninstall -g grunt

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

npm install -g grunt-cli

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

我想使用上面的 grunt 插件

但是当我运行 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
4

7 回答 7

177

一切都在gruntjs.com上得到了很好的解释。

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

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

npm install grunt --save-dev

选项--save-devgrunt作为开发依赖项添加到您的package.json中。这使得重新安装依赖项变得容易。

于 2013-03-19T18:42:46.930 回答
20

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

  1. 创建你的 package.json

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

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

    $ grunt 
    
于 2013-11-29T02:22:52.547 回答
4

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

于 2013-09-26T17:24:06.013 回答
4

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

于 2013-03-18T18:07:10.520 回答
1

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

于 2015-04-04T08:37:42.590 回答
1

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

sudo npm install -g grunt --save-dev

然后在项目文件夹中运行以下命令:

npm install

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

sudo npm uninstall -g grunt

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

npm install grunt --save-dev

并在项目文件夹中运行以下命令:

npm install

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

于 2019-09-17T15:25:35.107 回答
1

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

guntjs 入门第 2 步。

于 2016-11-10T08:36:47.237 回答