【问题标题】:Grunt install error/s: "should be installed with -g"Grunt 安装错误/s:“应该使用 -g 安装”
【发布时间】:2013-11-15 16:37:58
【问题描述】:

我正在尝试在我的机器上安装 Grunt。我已经阅读了一些tutorials 并关注了installation docs,但我无法让它工作。

CLI 使用此命令安装成功:

sudo npm install -g grunt-cli

当我选择一个本地目录(包含 package.json 和 Gruntfile)和 npm install 时,我看到以下消息:

npm WARN prefer global grunt-cli@0.1.9 should be installed with -g

为什么会这样?我确实使用-g 进行了安装 - 意思是“全球”,我理解。

在这之后,我似乎无法运行 grunt,例如:

$ grunt
-bash: grunt: command not found
$ grunt --version
-bash: grunt: command not found

我该如何解决这个问题?我错过了什么?

这是我的完整日志:

$ sudo npm install -g grunt-cli
Password:
npm http GET https://registry.npmjs.org/grunt-cli
npm http GET https://registry.npmjs.org/grunt-cli
npm http GET 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 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 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 GET https://registry.npmjs.org/abbrev
npm http GET https://registry.npmjs.org/glob
npm http GET https://registry.npmjs.org/lodash
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 GET https://registry.npmjs.org/abbrev
npm http GET https://registry.npmjs.org/glob
npm http GET https://registry.npmjs.org/lodash
npm http GET https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/graceful-fs
npm http GET https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/graceful-fs
npm http GET https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/graceful-fs
npm http GET https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/sigmund
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/sigmund
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/sigmund
/Users/tonyMac/.node/bin/grunt -> /Users/tonyMac/.node/lib/node_modules/grunt-cli/bin/grunt
grunt-cli@0.1.10 /Users/tonyMac/.node/lib/node_modules/grunt-cli
├── resolve@0.3.1
├── nopt@1.0.10 (abbrev@1.0.4)
└── findup-sync@0.1.2 (lodash@1.0.1, glob@3.1.21)
Tonys-iMac:~ tonyMac$ cd projects/testingGrunt
Tonys-iMac:testingGrunt tonyMac$ npm install
npm WARN prefer global grunt-cli@0.1.9 should be installed with -g

【问题讨论】:

    标签: node.js npm gruntjs


    【解决方案1】:

    当您安装您的语言环境包npm install 时,可能会将包请求grunt-cli 作为依赖项。别担心,这个警告并不意味着它破坏了任何东西。

    grunt: command not found 表示 grunt 二进制文件未添加到您的路径中。添加它,你会没事的:https://unix.stackexchange.com/questions/26047/how-to-correctly-add-a-path-to-path

    添加的路径应该是终端输出中的这个路径:/Users/tonyMac/.node/bin

    在 Bash 中:PATH=$PATH:/Users/tonyMac/.node/bin(如果您在命令行中运行它,则在前面添加 export 命令)。

    【讨论】:

    • 谢谢你,Simon - 这个命令正确吗? export PATH=~/opt/bin:$/Users/tonyMac/.node/bin
    • 不应该有$
    • (选择一个项目文件夹),我运行它然后npm install。这将返回 -bash: npm: command not found。我不明白怎么了?
    • 您覆盖完整路径,而不是扩展它。
    • 编辑了我的答案,详细说明了如何扩展 PATH 变量。
    【解决方案2】:

    你不应该使用sudo 来安装包。
    如果您使用sudo 安装 node 和 npm 时出现问题。
    我看到你使用 OSX,所以相信我,使用 brew 数据包管理器 (http://brew.sh) 安装节点并让它为你处理节点和 npm。

    首先删除节点,然后按照说明安装 Brew。 然后你可以输入brew install node,一切都像魅力一样。

    【讨论】:

    • 感谢 oozic!我删除了节点并重新安装了 brew(好东西!)。我尝试再次运行 grunt,但不幸的是我收到了一些错误,例如:ERR! Error: EACCES, unlinkRun command as admin。这听起来对吗?我可以提供更多代码。
    • 那么,您是使用管理员帐户来管理您的机器还是有一些限制?
    • 我有管理权限,这是唯一的用户帐户。也许某个文件/目录有读/写限制?
    • 我不知道您的整个安装历史。有点难,试试brew doctor看看有没有报错。至少您可以尝试删除所有 node/npm 二进制文件并从 brew 重新安装。
    • 感谢 oozic,我非常感谢这一点以及它是多么困难。为什么我们需要删除 al node/npm 二进制文件,我在安装 brew 之前就这样做了?我对brew doctor 没有错误,也更新了 brew。我看到的唯一错误是 Xcode 已过时(4.5.1),我会更新它并希望它有效。伙计,我只是想看看咕噜声。
    猜你喜欢
    • 2015-05-11
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2016-01-25
    • 2018-01-29
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多