“致命错误: 无法找到本地 grunt。”在运行“ grunt”命令时

我用下面的命令卸载了 grunt。

npm uninstall -g grunt

然后我用下面的命令再次安装了 grunt。

npm install -g grunt-cli

浏览以下连结: Https://npmjs.org/package/grunt-html

我想使用上面的咕噜声插件

但是当我运行 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
120507 次浏览

我认为你必须添加咕噜到你的 package.json文件。见 这个链接

所有这些都在 Gruntjs.com上得到了很好的解释。

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

因此,在您的项目文件夹中,您将需要安装(最好) 最新的咕噜声版本:

npm install grunt --save-dev

选项 --save-devgrunt作为 开发依赖添加到您的 包裹 Json。这使得它很容易重新安装依赖项。

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

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

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

我今天在 Windows 32位上遇到了同样的问题,节点是0.10.25,咕哝是0.4。

我跟随 Dongho 的回答,只有几个额外的步骤。 下面是我用来解决这个错误的步骤:

1)创建 package.json

$ npm init

2)为这个项目安装 grunt,这将在 node _ module/下安装。—— save-dev 将把这个模块添加到 package.json 中的 devDependency

$ npm install grunt --save-dev

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']);


};

在这里,src/**/*.jstest/**/*.js应该是您在项目中使用的实际 JS 文件的路径

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

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

6)运作 $ grunt

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

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

Guntjs 开始第二步。

这解决了我的问题。我错误地安装了 grunt 使用:

sudo npm install -g grunt --save-dev

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

npm install

这导致了问题的作者所看到的错误。 然后,我卸载咕噜使用:

sudo npm uninstall -g grunt

删除 node _ module 文件夹,并使用以下方法重新安装 grunt:

npm install grunt --save-dev

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

npm install

由于某些奇怪的原因,当您使用 -g 全局安装 grunt 然后卸载它时,node _ module 文件夹保留了一些内容,这些内容可以防止将 grunt 本地安装到项目文件夹中。