我如何安装grunt-cli而不会出错?

受到克里斯·科伊尔(Chris Coyier)的启发,我决定咕噜一声。 但是我遇到了很大的问题。

首先,我安装了Node。

然后我添加一个package.json文件到我的项目根目录,包括:

{ "name": "example-project", "version": "0.1.0", "devDependencies": { "grunt": "~0.4.1" } } 

然后我运行npm install

最后,我运行了npm install -g grunt-cli

其中回来了一大堆的错误:

 npm http GET https://registry.npmjs.org/grunt-cli npm http 304 https://registry.npmjs.org/grunt-cli npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli' npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'] npm ERR! errno: 3, npm ERR! code: 'EACCES', npm ERR! path: '/usr/local/lib/node_modules/grunt-cli', npm ERR! fstream_type: 'Directory', npm ERR! fstream_path: '/usr/local/lib/node_modules/grunt-cli', npm ERR! fstream_class: 'DirWriter', npm ERR! fstream_stack: npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23', npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53', npm ERR! 'Object.oncomplete (fs.js:107:15)' ] } npm ERR! npm ERR! Please try running this command again as root/Administrator. npm ERR! System Darwin 11.4.2 npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "grunt-cli" npm ERR! cwd /Users/bryce/repo npm ERR! node -v v0.10.23 npm ERR! npm -v 1.3.17 npm ERR! path /usr/local/lib/node_modules/grunt-cli npm ERR! fstream_path /usr/local/lib/node_modules/grunt-cli npm ERR! fstream_type Directory npm ERR! fstream_class DirWriter npm ERR! code EACCES npm ERR! errno 3 npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli' npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23 npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53 npm ERR! fstream_stack Object.oncomplete (fs.js:107:15) npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /Users/bryce/repo/npm-debug.log npm ERR! not ok code 0 Bryce:repo bryce$ npm install -g grunt-cli npm http GET https://registry.npmjs.org/grunt-cli npm http 304 https://registry.npmjs.org/grunt-cli npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli' npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'] npm ERR! errno: 3, npm ERR! code: 'EACCES', npm ERR! path: '/usr/local/lib/node_modules/grunt-cli', npm ERR! fstream_type: 'Directory', npm ERR! fstream_path: '/usr/local/lib/node_modules/grunt-cli', npm ERR! fstream_class: 'DirWriter', npm ERR! fstream_stack: npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23', npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53', npm ERR! 'Object.oncomplete (fs.js:107:15)' ] } npm ERR! npm ERR! Please try running this command again as root/Administrator. npm ERR! System Darwin 11.4.2 npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "grunt-cli" npm ERR! cwd /Users/bryce/repo npm ERR! node -v v0.10.23 npm ERR! npm -v 1.3.17 npm ERR! path /usr/local/lib/node_modules/grunt-cli npm ERR! fstream_path /usr/local/lib/node_modules/grunt-cli npm ERR! fstream_type Directory npm ERR! fstream_class DirWriter npm ERR! code EACCES npm ERR! errno 3 npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli' npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23 npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53 npm ERR! fstream_stack Object.oncomplete (fs.js:107:15) npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /Users/bryce/repo/npm-debug.log npm ERR! not ok code 0 

我认为上述错误是当我试图运行grunt ,我只是得到-bash: grunt: command not found

那里的任何人知道发生了什么?

所需要的只是添加了“sudo”,因为我没有默认安装grunt-cli的权限。 一旦我运行下面的命令,并input我的密码,它按预期工作。

 sudo npm install -g grunt-cli 

希望这可以帮助别人!

编辑:下面引用的文章是相当古老的,指的是v0.3之前的哟! 目前,如果npm使用sudo权限运行,它将在执行命令之前降级为nobody用户。

根据npm的维护者的说法,使用sudo安装软件包被认为是不好的做法,因为你允许软件包完全控制你的系统,你不能也不应该信任这些软件包的root权限。

http://howtonode.org/introduction-to-npm

由于您不应该使用sudo进行安装 – 您将在安装软件包之前执行此操作,不会再遇到权限错误:

sudo chown -R $ USER / usr / local

来源: https : //howtonode.org/introduction-to-npm

我会build议在你的项目中安装grunt(而不是使用-g选项)。

例如,要安装Grunt命令行工具,请使用:

 cd ~/path-to-my-project npm install grunt-cli --save-dev 

如果你想运行咕噜,那么你使用:

 cd ~/path-to-my-project ./node_modules/./bin/grunt 

通过在您的项目中安装grunt-cli,您将完全控制安装哪个版本,而不必依赖于不一致的PATHvariables。

我也遇到了这个问题,在读完输出后,我发现我安装的node.js版本与当前版本的grunt-cli不兼容。

更新的node.js问题解决了。

在项目文件夹中安装grunt的命令是:

 npm install grunt --save-dev