简体   繁体   English

没有sudo不能更改npm install -g

[英]Cannot npm install -g without sudo changing prefix

I have done this: https://stackoverflow.com/a/21712034 我已经做到了: https : //stackoverflow.com/a/21712034

now my npm config is the following: 现在我的npm配置如下:

 npm config get prefix
 /home/ranu/npm

But when I run npm install -g grunt-cli I get: 但是当我运行npm install -g grunt-cli我得到:

npm ERR! Error: EACCES, mkdir '/usr/lib/node_modules/grunt-cli'
npm ERR!  { [Error: EACCES, mkdir '/usr/lib/node_modules/grunt-cli']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/usr/lib/node_modules/grunt-cli',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/usr/lib/node_modules/grunt-cli',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack: 
npm ERR!    [ '/usr/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23', 
npm ERR!      '/usr/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 Linux 3.11.0-26-generic
npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "-g" "grunt-cli"
npm ERR! cwd /home/ranu
npm ERR! node -v v0.10.29
npm ERR! npm -v 1.4.14
npm ERR! path /usr/lib/node_modules/grunt-cli
npm ERR! fstream_path /usr/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/lib/node_modules/grunt-cli'
npm ERR! fstream_stack /usr/lib/node_modules/npm/node_modules/fstream/lib/dir-      writer.js:36:23
npm ERR! fstream_stack /usr/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!     /home/ranu/npm-debug.log
npm ERR! not ok code 0

Why is trying to install it on /usr/lib/node_modules/ if the prefix is set to /home/ranu/npm ? 如果将前缀设置为/ home / ranu / npm,为什么还要尝试将其安装在/ usr / lib / node_modules /上?

UPDATE: I could it make it work with the following command: 更新:我可以使它与以下命令一起使用:

npm install --prefix="/home/ranu/npm" -g grunt-cli

There is any way to make it as a default? 有什么方法可以使其默认?

Every time I get a new Machine, I run into this. 每次我买一台新机器时,都会遇到这个问题。

I'm citing this url fixing-npm-permissions because it's actually NPM's doc, but also because it was the only method that fixed my problem. 我引用此网址为fix-npm-permissions,是因为它实际上是NPM的文档,也是因为它是解决我的问题的唯一方法。 Not the first search result but the simplest fix for me. 不是第一个搜索结果,而是最简单的修复方法。 I'm sure there are more robust fixes. 我确定还有更强大的修复程序。

Rather than copying and pasting, I'm recommending you just go read it. 建议您不要阅读并复制而不是粘贴。

I followed step 1, and though according to them it should have worked. 我按照步骤1进行操作,尽管根据他们的意见,它应该可以工作。 But, I ended up also using step 2 for the win. 但是,我最终还是使用了第2步来获胜。

您可以使用NVM (节点版本管理器),而无需再为npm commands而烦恼sudo了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM