在Windows 7专业版上出现“致命错误:无法找到本地Grunt。”

3
如果我想启动grunt,我会收到以下信息:
c:\repositories\kunde_1\themes-projekt_1\projekt_1-responsive\source>grunt
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

运行grunt时,您会看到:

C:\Users\dev_user>npm install -g grunt-cli
npm http GET https://registry.npmjs.org/grunt-cli
npm http 304 https://registry.npmjs.org/grunt-cli
npm http GET https://registry.npmjs.org/nopt
npm http GET https://registry.npmjs.org/findup-sync
npm http GET https://registry.npmjs.org/resolve
npm http 304 https://registry.npmjs.org/nopt
npm http 304 https://registry.npmjs.org/findup-sync
npm http 304 https://registry.npmjs.org/resolve
npm http GET https://registry.npmjs.org/abbrev
npm http GET https://registry.npmjs.org/glob
npm http GET https://registry.npmjs.org/lodash
npm http 304 https://registry.npmjs.org/abbrev
npm http 304 https://registry.npmjs.org/glob
npm http 304 https://registry.npmjs.org/lodash
npm http GET https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/minimatch
npm http 304 https://registry.npmjs.org/inherits
npm http 304 https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/sigmund
npm http 304 https://registry.npmjs.org/lru-cache
npm http 304 https://registry.npmjs.org/sigmund
C:\Users\dev_user\AppData\Roaming\npm\grunt -> C:\Users\dev_user\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt
grunt-cli@0.1.13 C:\Users\dev_user\AppData\Roaming\npm\node_modules\grunt-cli
+-- nopt@1.0.10 (abbrev@1.0.7)
+-- resolve@0.3.1
+-- findup-sync@0.1.3 (lodash@2.4.2, glob@3.2.11)

进入用户目录后输入grunt:

C:\Users\dev_user>grunt
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

但是当我进入项目目录时,同样的消息出现了!

操作系统:Windows 7 Professional
npm版本:1.3.5
node版本:v0.10.15

C:\Users\dev_user>npm ls -g
C:\Users\dev_user\AppData\Roaming\npm
+-- coffee-script@1.9.3
+-- grunt-cli@0.1.13
  +-- findup-sync@0.1.3
  ¦ +-- glob@3.2.11
  ¦ ¦ +-- inherits@2.0.1
  ¦ ¦ +-- minimatch@0.3.0
  ¦ ¦   +-- lru-cache@2.6.5
  ¦ ¦   +-- sigmund@1.0.1
  ¦ +-- lodash@2.4.2
  +-- nopt@1.0.10
  ¦ +-- abbrev@1.0.7
  +-- resolve@0.3.1

我设置的环境变量:

变量名称: grunt 路径: C:\Users\dev_user\AppData\Roaming\npm\grunt 之后启动了新电脑

有人知道为什么会出现错误信息Fatal error: Unable to find local grunt.吗?

2个回答

5
安装grunt-cli并不会安装Grunt任务运行器。 grunt-cli是命令行接口,允许在同一台机器上同时安装多个版本的Grunt。
要安装grunt,您需要通过以下命令全局安装它:
npm install -g grunt

或者通过以下命令在本地运行:

npm install grunt

您可以在官方文档中了解有关grunt-cli的信息。

@Matthew Bakaitis的注释:

不建议全局安装grunt

这是一个好习惯,因为grunt-cli首先加载全局版本(如果我没有记错)。 如果项目期望特定版本的grunt,则全局安装会破坏其预期。 此外,如果您的构建依赖于grunt,为什么不将其安装为依赖项并在package.json中列出? 如果您没有在package.json中添加依赖项,则存在其他开发人员或目标系统会全局安装grunt的巨大假设。鉴于grunt的使用情况,我想不到许多情况下全局安装会更好。


FYI:全局安装 grunt不被鼓励的 - Matthew Bakaitis
@MatthewBakaitis 你能解释一下为什么吗? - Artem Baranovskii
这是一个好的实践,因为 grunt-cli 首先加载全局版本(如果我没有记错的话)。如果项目期望某个特定版本的 grunt,则全局安装会破坏此功能。此外,如果您依赖于 grunt 进行构建,为什么不将其安装为依赖项并在 package.json 中列出呢?假设其他开发人员或目标系统将全局安装 grunt 是一个巨大的假设,如果您没有将其添加为 package.json 的依赖项。鉴于 grunt 的使用情况,我想不到许多情况下全局安装会更好。 - Matthew Bakaitis
谢谢。我已将你的注释添加到答案中。 - Artem Baranovskii

1
我的问题的解决方案。 在我的情况下,我的同事找到了解决方案。 我安装了两个版本的NodeJS,一个旧版本1.0.35和当前版本1.2.7。 在Windows卸载程序中只有一个版本可见! 在卸载了那个版本和所有目录之后,即在当前版本被安装之前,旧版本和新版本都被删除了,因为我可以使用命令npm install。 之后,我可以轻松地调用grunt,并且它正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接