使用root用户的NPM安装权限被拒绝错误

时间:2018-08-20 01:53:25

标签: node.js ubuntu npm frontend npm-install

因此,我已经使用root用户使用NVM在本地计算机上全新安装了npm / node,一切看起来都很好,现在我的问题是当我尝试使用 npm install --unsafe-perm安装-在我的终端上显示我的项目文件夹上的详细命令错误。

npm verb stack Error: Command failed: /usr/bin/git clone --depth=1 -q -b 0.0.7 https://github.com/Mango/emitter.git /root/.npm/_cacache/tmp/git-clone-28a98ad9
npm verb stack fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-28a98ad9': Permission denied
npm verb stack 
npm verb stack     at ChildProcess.exithandler (child_process.js:282:12)
npm verb stack     at ChildProcess.emit (events.js:182:13)
npm verb stack     at maybeClose (internal/child_process.js:957:16)
npm verb stack     at Socket.stream.socket.on (internal/child_process.js:378:11)
npm verb stack     at Socket.emit (events.js:182:13)
npm verb stack     at Pipe._handle.close [as _onclose] (net.js:598:12)
npm verb cwd /web/nbltv
npm verb Linux 4.15.0-29-generic
npm verb argv "/root/.nvm/versions/node/v10.1.0/bin/node" "/root/.nvm/versions/node/v10.1.0/bin/npm" "install" "--unsafe-per" "-verbose"
npm verb node v10.1.0
npm verb npm  v5.6.0
npm ERR! code 128
npm ERR! Command failed: /usr/bin/git clone --depth=1 -q -b 0.0.7 https://github.com/Mango/emitter.git /root/.npm/_cacache/tmp/git-clone-28a98ad9
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-28a98ad9': Permission denied
npm ERR! 
npm verb exit [ 1, true ]

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-08-20T01_36_33_496Z-debug.log

NPM版本-5.6 NODE版本-10.1

任何帮助都会得到极大的帮助。谢谢!

4 个答案:

答案 0 :(得分:3)

添加到您的node_modules中,并且不要以sudo的身份运行。

您应该始终拥有.npm目录的所有权,因为 NPM会拒绝使用sudo安装本机模块,因为这会带来安全风险。

尝试sudo chown -R $(whoami) ~/.npm,看看是否可行。

或者按照reinstall with a node version manager中的建议,docs不带root:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

答案 1 :(得分:3)

在用户空间中创建新的全局存储是防止此错误的推荐方法。不要使用sudo或更改位置所有权。

$ mkdir ~/.npm-global
$ npm config set prefix '~/.npm-global'
$ export PATH=~/.npm-global/bin:$PATH
$ source ~/.profile

但是,由于您使用的是npm 5.6,因此这可能与您更相关:

  

npx:运行全局命令的替代方法

     

如果您使用的是npm 5.2或更高版本,则可能要考虑将npx作为运行全局命令的替代方法,尤其是在您仅偶尔需要命令的情况下。有关更多信息,请参见this article about npx

来源:https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

答案 2 :(得分:0)

@ U-way答案非常好,但对于使用nvm的人来说 而不是:

sudo chown -R $(whoami) ~/.npm

sudo chown -R $(whoami) ~/.nvm

这对我有效

答案 3 :(得分:0)

这对我有用

sudo chown -R $(whoami) ~/.npm