node.js 没有 sudo,NPM 模块将无法全局安装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19352976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:58:42  来源:igfitidea点击:

NPM modules won't install globally without sudo

node.jsubuntunpmyeomannode-modules

提问by HeberLZ

I have just reinstalled Ubuntu 12.04 LTS, and before anything else i did these steps:

我刚刚重新安装了 Ubuntu 12.04 LTS,在执行其他任何操作之前,我执行了以下步骤

  1. Installed Node via package manager with the following script

    sudo apt-get update
    
    sudo apt-get install python-software-properties python g++ make
    
    sudo add-apt-repository ppa:chris-lea/node.js
    
    sudo apt-get update
    
    sudo apt-get install nodejs
    
  2. Tried to install yeoman, express, n, yeoman's generators globally and all of them returned the same error

    npm ERR! Error: EACCES, symlink '../lib/node_modules/n/bin/n'

    npm ERR! { [Error: EACCES, symlink '../lib/node_modules/n/bin/n'] errno: 3, code: 'EACCES', path: '../lib/node_modules/n/bin/n' }

    npm ERR!

    npm ERR! Please try running this command again as root/Administrator.

    npm ERR! System Linux 3.8.0-29-generic

    npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "-g" "-d" "n"

    npm ERR! cwd /home/heberlz

    npm ERR! node -v v0.10.20

    npm ERR! npm -v 1.3.11

    npm ERR! path ../lib/node_modules/n/bin/n

    npm ERR! code EACCES

    npm ERR! errno 3

    npm ERR! stack Error: EACCES, symlink '../lib/node_modules/n/bin/n'

    npm ERR!

    npm ERR! Additional logging details can be found in:

    npm ERR! /home/heberlz/npm-debug.log

    npm ERR! not ok code 0

  3. Reclaimed ownership of the following folders recursively~/.npm, /usr/lib/node, /usr/lib/node_modules, and of the following symlinks /usr/bin/node, /usr/bin/nodejs with absolutely no success

  1. 使用以下脚本通过包管理器安装节点

    sudo apt-get update
    
    sudo apt-get install python-software-properties python g++ make
    
    sudo add-apt-repository ppa:chris-lea/node.js
    
    sudo apt-get update
    
    sudo apt-get install nodejs
    
  2. 尝试全局安装 yeoman、express、n、yeoman 的生成器,但都返回了相同的错误

    错误!错误:EACCES,符号链接'../lib/node_modules/n/bin/n'

    错误!{ [错误:EACCES,符号链接'../lib/node_modules/n/bin/n'] errno:3,代码:'EACCES',路径:'../lib/node_modules/n/bin/n'}

    错误!

    错误!请尝试以 root/管理员身份再次运行此命令。

    错误!系统 Linux 3.8.0-29-generic

    错误!命令“/usr/bin/node”“/usr/bin/npm”“安装”“-g”“-d”“n”

    错误!cwd /home/heberlz

    错误!节点 -v v0.10.20

    错误!npm -v 1.3.11

    错误!路径 ../lib/node_modules/n/bin/n

    错误!代码 EACCES

    错误!错误 3

    错误!堆栈错误:EACCES,符号链接'../lib/node_modules/n/bin/n'

    错误!

    错误!可以在以下位置找到其他日志记录详细信息:

    错误!/home/heberlz/npm-debug.log

    错误!不行 代码 0

  3. 递归地收回以下文件夹的所有权~/.npm、/usr/lib/node、/usr/lib/node_modules 以及以下符号链接 /usr/bin/node、/usr/bin/nodejs 的所有权,但绝对没有成功

I need to install yeoman and its generators without sudo not to be in trouble later on :(

我需要在没有 sudo 的情况下安装 yeoman 及其生成器,以免以后遇到麻烦:(

回答by ErikAndreas

Ubuntu 12.04 and using Chris Lea's PPA for install the following works for me:

Ubuntu 12.04 并使用 Chris Lea 的 PPA 安装以下对我有用:

npm config set prefix '~/.npm-packages'

and adding $HOME/.npm-packages/bin to $PATH

并将 $HOME/.npm-packages/bin 添加到 $PATH

append to .bashrc

附加到 .bashrc

export PATH="$PATH:$HOME/.npm-packages/bin"

see https://stackoverflow.com/a/18277225from @passy

从@passy看到https://stackoverflow.com/a/18277225

回答by Tony O'Hagan

If you already have $HOME/binin your path, a simpler solution is just ...

如果你已经$HOME/bin在你的道路上,一个更简单的解决方案就是......

npm config set prefix ~
  • New node commands will now install into your $HOME/bindirectory.
  • No need to change your path!
  • 新的节点命令现在将安装到您的$HOME/bin目录中。
  • 无需更改路径!

Since this discussion is really about reducing the security risks of running sudo, you should also be aware that any node app could potentially be installing an app name that does not match the registered node package name you think you're installing. So there is a security risk that an npm installwill replace an existing system command or one you already have in $HOME/bin. If you're concerned, check the bin, and scriptsproperties in the package.jsonfile of the app you're installing first.

由于此讨论实际上是关于降低运行 的安全风险sudo,因此您还应该注意,任何节点应用程序都可能安装与您认为正在安装的已注册节点包名称不匹配的应用程序名称。因此存在安全风险,即npm install将替换现有的系统命令或您已经在$HOME/bin. 如果您担心,请先检查您要安装的应用程序文件中的binscripts属性package.json

In general, it's safest to:

一般来说,最安全的做法是:

  • (a) Place $HOME/binlast in your path so system commands are not superseded.
  • (b) don't include "." or any relative path in your $PATHso you don't accidentally run a command that happens to be in the current directory.
  • (a)$HOME/bin放在路径的最后,这样系统命令就不会被取代。
  • (b) 不包括“。” 或者您的任何相对路径,$PATH这样您就不会意外运行恰好在当前目录中的命令。

Reference:

参考:

回答by Andrei Karpushonak

As for October 2014:

至于 2014 年 10 月:

Node.js is available from the NodeSource Debian and Ubuntu binary distributions repository.

Node.js 可从 NodeSource Debian 和 Ubuntu 二进制发行版存储库获得

curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs

That's it.

就是这样。

Outdated answer:

过时的答案:

The fastest way without using sudo is like described here by isaac

不使用 sudo 的最快方法就像isaac 在此处描述的那样

I strongly encourage you not to do package management with sudo! Packages can run arbitrary scripts, which makes sudoing a package manager command as safe as a chainsaw haircut. Sure, it's fast and definitely going to cut through any obstacles, but you might actually want that obstacle to stay there.

I recommend doing this once instead:

我强烈建议您不要使用 sudo 进行包管理!包可以运行任意脚本,这使得 sudo 包管理器命令就像电锯理发一样安全。当然,它很快并且肯定会穿过任何障碍,但您实际上可能希望该障碍留在那里。

我建议这样做一次:

sudo chown -R $USER /usr/local

EDIT:

编辑:

There are certain security concerns and functionality limitations regarding changing the ownership of /usr/local to the current user:

将 /usr/local 的所有权更改为当前用户存在某些安全问题和功能限制:

Having said that, if you want to install global module without using sudo, I don't see any better solution (from pragmatic point of view) than mentioned. Security vs easy of use is very broad topic, and there is no easy answer for that - it just depends on your requirements.

话虽如此,如果您想在不使用 sudo 的情况下安装全局模块,我认为没有比上面提到的更好的解决方案(从实用的角度来看)。安全性与易用性是一个非常广泛的话题,对此没有简单的答案——这取决于您的要求。

回答by HeberLZ

The issue was i installed node using sudo, to avoid errors when installing npm modules globally one MUST NEVER install node with sudo.

问题是我使用 sudo 安装了节点,以避免在全局安装 npm 模块时出错,永远不要使用 sudo 安装节点。

My solution was to reinstall nodeit this way:

我的解决方案是以这种方式重新安装节点

Download latest stable node sources from nodejs.org #in my case node-v0.10.20.tar.gz

从 nodejs.org 下载最新的稳定节点源#in my case node-v0.10.20.tar.gz

tar -zxf node-v0.10.20.tar.gz #uncompress sources

cd node-v0.10.20 #enter uncompressed folder

sudo chown -R $USER /usr/local

./configure --prefix=/usr/local && make && make install

tar -zxf node-v0.10.20.tar.gz #解压源

cd node-v0.10.20 #进入解压文件夹

须藤 chown -R $USER /usr/local

./configure --prefix=/usr/local && make && make install

One thing to note is that only taking ownership of the /usr/local folder wouldn't work in my case because node installation itself was made with sudo

需要注意的一件事是,仅获得 /usr/local 文件夹的所有权在我的情况下不起作用,因为节点安装本身是使用 sudo 进行的

Last step to install yeoman: #although at yeoman.io it says that doing "npm install -g yo" already installs bower and grunt, there are some submodules of grunt that fail, so i fixed that by installing it by itself

安装 yeoman 的最后一步:#although at yeoman.io 它说执行“npm install -g yo”已经安装了 bower 和 grunt,有一些 grunt 的子模块失败,所以我通过自己安装来解决这个问题

npm install -g bower

npm install -g grunt

npm install -g yo

npm install -g generator-angular

npm install -g bower

npm install -g grunt

npm install -g yo

npm install -g 生成器角度

回答by santervo

I solved this problem with environment variable and shell alias:

我用环境变量和shell别名解决了这个问题:

export NPM_PREFIX=$HOME/node
alias npmg="npm -g --prefix $NPM_PREFIX"

For me npm did not honor the "prefix" config setting in .npmrc.

对我来说,npm 不支持 .npmrc 中的“前缀”配置设置。

回答by Monis Majeed

Find the path to npm's directory:

找到 npm 目录的路径:

npm config get prefix

For many systems, this will be /usr/local.

对于许多系统,这将是 /usr/local。

Change the owner of npm's directories to the name of the current user (your username!):

将 npm 目录的所有者更改为当前用户的名称(您的用户名!):

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).

这会更改 npm 和其他一些工具(lib/node_modules、bin 和 share)使用的子文件夹的权限。

Here is the link for full details

这是完整详细信息的链接

https://docs.npmjs.com/getting-started/fixing-npm-permissions

https://docs.npmjs.com/getting-started/fixing-npm-permissions

回答by prasanthv

According to this similar SO post: npm throws error without sudo

根据这个类似的SO帖子:npm throws error without sudo

Looks like you might have an ownership issue with ~/.npmdirectory.

看起来您的~/.npm目录可能存在所有权问题。

As with the answer in that one, try:

与那个答案一样,请尝试:

sudo chown -R `whoami` ~/.npm

回答by Iam Zesh

If you're on a developping machine, you might be better off considering using nvm.

如果您使用的是开发机器,则最好考虑使用nvm

If not, you simply want to install using your favorite package manager.

如果没有,您只想使用您喜欢的包管理器进行安装。

Whatever the case may be, I'd recommend checking this answer on stackoverflow

不管是什么情况,我都建议在stackoverflow上查看这个答案

回答by Vinggui

Actually, I just changed the permission of a user folder that was owned by root:

实际上,我只是更改了 root 拥有的用户文件夹的权限:

sudo chown -R $USER ~/.config/configstore

Then I could "npm install" and "bower install" without sudo! Worked fine!

然后我可以在没有 sudo 的情况下“npm install”和“bower install”!工作得很好!

回答by Conor

using lubuntu 14.04.3, I tried changing ownership of .npm and npm prefix, updated my path, npm installed modules to my home directory without sudo but the path was incorrect so the modules like ember were not found, linuxbewsolved the problem, quick setup guide herefor node/npm

使用 lubuntu 14.04.3,我尝试更改 .npm 和 npm 前缀的所有权,更新我的路径,npm 安装模块到我的主目录,没有 sudo 但路径不正确所以没有找到像 ember 这样的模块,linuxbew解决了这个问题,很快此处为 node/npm 的设置指南