如何在不出错的情况下安装grunt-cli?

Bry*_*son 8 bash node.js gruntjs

在克里斯科伊尔的帖子的启发下,我决定放弃咕噜咕噜.但是我在设置方面遇到了很大的问题.

首先,我安装了Node.

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

{
  "name": "example-project",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我跑了 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
Run Code Online (Sandbox Code Playgroud)

我认为上述错误是我尝试运行的原因grunt,我只是得到了-bash: grunt: command not found

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

Bry*_*son 14

所有这一切都是添加'sudo',因为我没有默认安装grunt-cli的权限.运行以下命令并输入密码后,它按预期工作.

sudo npm install -g grunt-cli
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人!


srq*_*inn 9

编辑:下面引用的文章很老,指的是v0.3之前的呀!目前,如果npmsudo权限运行,它将在执行命令之前降级为nobody用户.

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

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

  • 注意:[自NPM 0.3起推荐] [以[sudo`为单位]安装[https://github.com/npm/npm#permissions-when-using-npm-to-install-other-stuff)([ 2011年2月](https://github.com/npm/npm/tree/v0.3.0)).它将作为"nobody"用户运行它们.大多数相反的信息来自此前的0.1和0.2的合并年份. (2认同)