node.js npm 在没有 sudo 的情况下抛出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16151018/
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 14:16:45  来源:igfitidea点击:

npm throws error without sudo

node.jsunixpermissionsnpmsudo

提问by Chad

I just installed node and npm through the package on nodejs.org and whenever I try to search or install something with npm it throws the following error, unless I sudo the command. I have a feeling this is a permissions issue? I am already the admin.

我刚刚通过 nodejs.org 上的包安装了 node 和 npm,每当我尝试使用 npm 搜索或安装某些东西时,它都会引发以下错误,除非我 sudo 命令。我有一种感觉,这是一个权限问题?我已经是管理员了。

npm ERR! Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json'
npm ERR!  { [Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/Users/chietala/.npm/-/all/.cache.json' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 12.2.0
npm ERR! command "node" "/usr/local/bin/npm" "search" "bower"
npm ERR! cwd /Users/chietala
npm ERR! node -v v0.10.4
npm ERR! npm -v 1.2.18
npm ERR! path /Users/chietala/.npm/-/all/.cache.json
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json'
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/chietala/npm-debug.log
npm ERR! not ok code 0

回答by Noah

This looks like a permissions issue in your home directory. To reclaim ownership of the .npmdirectory execute:

这看起来像是您的主目录中的权限问题。要收回.npm目录的所有权,执行:

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

回答by Yves M.

Permissions you used when installing Node will be required when doing things like writing in your npm directory(npm link, npm install -g, etc.).

你使用权限时安装节点会做这样的事情在你的写作时需要NPM目录npm linknpm install -g,等)。

You probably ran node installation with root permissions, that's why the global package installation is asking you to be root.

您可能使用 root 权限运行节点安装,这就是全局包安装要求您成为 root 的原因。



Solution 1: NVM

解决方案 1:NVM

Don't hack with permissions, install node the right way.

不要破解权限,以正确的方式安装节点。

On a development machine, you should not install and run node with root permissions, otherwise things like npm link, npm install -gwill need the same permissions.

在开发机器上,你不应该使用 root 权限安装和运行 node,否则像npm link,npm install -g将需要相同的权限。

NVM(Node Version Manager) allows you to install Node without root permissions and also allows you to install many versions of Node to play easily with them.. Perfect for development.

NVM(Node 版本管理器)允许您在没有 root 权限的情况下安装 Node,还允许您安装多个版本的 Node 以轻松使用它们......非常适合开发。

  1. Uninstall Node (root permission will probably be required). Thismight help you.
  2. Then install NVMfollowing instructions on this page.
  3. Install Node via NVM: nvm install node
  1. 卸载 Node(可能需要 root 权限)。可能对你有帮助。
  2. 然后按照此页面上的说明安装NVM
  3. 通过 NVM 安装节点: nvm install node

Now npm link, npm install -gwill no longer require you to be root.

现在npm linknpm install -g将不再需要您是 root。

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

编辑:另见https://docs.npmjs.com/getting-started/fixing-npm-permissions



Solution 2: Install packages globally for a given user

解决方案 2:为给定用户全局安装软件包

Don't hack with permissions, install npm packages globally the right way.

不要用权限破解,以正确的方式全局安装 npm 包。

If you are on OSX or Linux, you can create a user dedicated directory for your global packageand setup npmand nodeto know how to find globally installed packages.

如果您使用的是 OSX 或 Linux,您可以为全局包和设置创建一个用户专用目录,npmnode了解如何查找全局安装的包。

Check out this great articlefor step by step instructions on installing npm modules globally without sudo.

查看这篇很棒的文章,了解如何在没有 sudo 的情况下全局安装 npm 模块的分步说明。

See also: npm's documentation on Fixing npm permissions.

另请参阅:关于修复 npm 权限的npm 文档。

回答by Xilo

Also you will need the write permission in node_modulesdirectory:

您还需要node_modules目录中的写权限:

sudo chown -R $USER /usr/local/lib/node_modules

回答by Joel Purra

Changing the owner on "system-global" folders is a hack. On a fresh install, I would configure NPM to use an already writable locationfor "user-global" programs:

更改“系统全局”文件夹的所有者是一种技巧。在全新安装时,我会将NPM 配置为对“用户全局”程序使用已经可写的位置

npm config set prefix ~/npm

Then make sure you add that folder to your path:

然后确保将该文件夹添加到路径中:

export PATH="$PATH:$HOME/npm/bin"

See @ErikAndreas' answerto NPM modules won't install globally without sudo and longer step-by-step guideby @sindresorhuswith also sets $MANPATH.

@ErikAndreas回答NPM模块将不会在全球没有sudo安装 再一步一步的指导,通过@sindresorhus与还套$MANPATH

回答by danomarr

I encountered this when installing Recess (https://github.com/twitter/recess) to compile my CSS for Bootstrap 3.

我在安装 Recess ( https://github.com/twitter/recess) 为 Bootstrap 3 编译 CSS时遇到了这个问题。

When installing recess:

安装凹槽时:

-npm install recess -g
  1. You need to unlock permissions in your homedirectory, like Noahsays:

    sudo chown -R `whoami` ~/.npm
    
  2. You also need write permissions to the node_modulesdirectory, like Xilosays, so if it still isn't working, try:

    sudo chown -R `whoami` /usr/local/lib/node_modules
    
  3. If you are still seeing errors, you may also need to correct /usr/localpermissions:

    sudo chown -R `whoami` /usr/local
    
  1. 你需要解锁home目录中的权限,就像诺亚说的:

    sudo chown -R `whoami` ~/.npm
    
  2. 您还需要对该node_modules目录的写权限,如Xilo所说,所以如果它仍然无法正常工作,请尝试:

    sudo chown -R `whoami` /usr/local/lib/node_modules
    
  3. 如果您仍然看到错误,您可能还需要更正/usr/local权限

    sudo chown -R `whoami` /usr/local
    

Please note that as indicated in this post/usr/local/isn't actually a system dir if you are on a Mac, so, this answer is actually perfectly "safe" for Mac users. However, if you are on Linux, see Christopher Will's answer below for a multi-user friendly, system dir safe (but more complex) solution.

请注意,如本文/usr/local/所述,如果您使用的是 Mac,那么它实际上并不是系统目录,因此,对于 Mac 用户来说,这个答案实际上是完全“安全的”。但是,如果您使用的是 Linux,请参阅下面的Christopher Will的回答,以了解多用户友好、系统目录安全(但更复杂)的解决方案。

回答by suther

Watch OUT!!!Watch OUT!!!Watch OUT!!!

小心!!!小心!!!小心!!!

chown or chmod is NOTthe solution, because of security-risk.

由于安全风险,chown 或 chmod不是解决方案。

Instead do this, do:

而是这样做,请执行以下操作:

First check, where npm point to, if you call:

首先检查 npm 指向的位置,如果您调用:

npm config get prefix

If /usr is returned, you can do the following:

如果返回 /usr,您可以执行以下操作:

mkdir ~/.npm-global
export NPM_CONFIG_PREFIX=~/.npm-global
export PATH=$PATH:~/.npm-global/bin

This create a npm-Directory in your Home-Directory and point npmto it.

这会在您的 Home-Directory 中创建一个 npm-Directory 并指向npm它。

To got this changes permanent, you have to add the export-command to your .bashrc:

要永久更改此更改,您必须将 export-command 添加到 .bashrc 中:

echo -e "export NPM_CONFIG_PREFIX=~/.npm-global\nexport PATH=$PATH:~/.npm-global/bin" >> ~/.bashrc

回答by Christopher Will

Other answers are suggesting to change ownerships or permissions of system directories to a specific user. I highly disadvise from doing so, this can become very awkward and might mess up the entire system!

其他答案建议将系统目录的所有权或权限更改为特定用户。我强烈建议不要这样做,这会变得非常尴尬,并且可能会弄乱整个系统!

Here is a more generic and safer approach that supports multi-user as well.

这是一种更通用、更安全的方法,它也支持多用户。

Create a new group for node-users and add the required users to this group. Then set the ownership of node-dependant files/directories to this group.

为节点用户创建一个新组并将所需的用户添加到该组。然后将依赖于节点的文件/目录的所有权设置为该组。

# Create new group
sudo groupadd nodegrp 

# Add user to group (logname is a variable and gets replaced by the currently logged in user)
sudo usermod -a -G nodegrp `logname`

# Instant access to group without re-login
newgrp nodegrp

# Check group - nodegrp should be listed as well now
groups

# Change group of node_modules, node, npm to new group 
sudo chgrp -R nodegrp /usr/lib/node_modules/
sudo chgrp nodegrp /usr/bin/node
sudo chgrp nodegrp /usr/bin/npm

# (You may want to change a couple of more files (like grunt etc) in your /usr/bin/ directory.)

Now you can easily install your modules as user

现在您可以轻松地以用户身份安装您的模块

npm install -g generator-angular

Some modules (grunt, bower, yo etc.) will still need to be installed as root. This is because they create symlinks in /user/bin/.

某些模块(grunt、bower、yo 等)仍需要以 root 身份安装。这是因为他们在 /user/bin/ 中创建了符号链接。

Edit

编辑

3 years later I'd recommend to use Node Version Manager. It safes you a lot of time and trouble.

3 年后,我建议使用Node Version Manager。它可以为您节省大量时间和麻烦。

回答by HoppyKamper

The official documentationon how to fix npm installpermissions with an EACCESerror is located at https://docs.npmjs.com/getting-started/fixing-npm-permissions.

关于如何修复错误权限的官方文档位于https://docs.npmjs.com/getting-started/fixing-npm-permissionsnpm installEACCES

I encountered this problem after a fresh install of node using the .pkginstaller on OSX. There are some great answers here, but I didn't see a link to npmjs.com yet.

.pkg在 OSX 上使用安装程序全新安装节点后,我遇到了这个问题。这里有一些很好的答案,但我还没有看到 npmjs.com 的链接。

Option 1: Change the permission to npm's default directory

选项 1:更改 npm 默认目录的权限

  1. Find the path to npm's directory:

    npm config get prefix
    
  1. 找到 npm 目录的路径:

    npm config get prefix
    

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

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

WARNING: If the displayed path is just /usr, switch to Option 2.

警告:如果显示的路径只是/usr,请切换到选项 2

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

    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).

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

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

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

Option 2: Change npm's default directory to another directory

选项 2:将 npm 的默认目录更改为另一个目录

There are times when you do not want to change ownership of the default directory that npm uses (i.e. /usr) as this could cause some problems, for example if you are sharing the system with other users.

有时您不想更改 npm 使用的默认目录(即/usr)的所有权,因为这可能会导致一些问题,例如,如果您与其他用户共享系统。

Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.

相反,您可以将 npm 配置为完全使用不同的目录。在我们的例子中,这将是我们主文件夹中的一个隐藏目录。

  1. Make a directory for global installations:

    mkdir ~/.npm-global
    
  2. Configure npm to use the new directory path:

    npm config set prefix '~/.npm-global'
    
  3. Open or create a ~/.profile file and add this line:

    export PATH=~/.npm-global/bin:$PATH
    
  4. Back on the command line, update your system variables:

    source ~/.profile
    
  1. 为全局安装创建一个目录:

    mkdir ~/.npm-global
    
  2. 配置 npm 以使用新的目录路径:

    npm config set prefix '~/.npm-global'
    
  3. 打开或创建一个 ~/.profile 文件并添加以下行:

    export PATH=~/.npm-global/bin:$PATH
    
  4. 回到命令行,更新您的系统变量:

    source ~/.profile
    

回答by ptim

As if we need more answers here, but anyway..

好像我们在这里需要更多答案,但无论如何..

Sindre Sorus has a guide Install npm packages globally without sudo on OS X and Linuxoutlining how to cleanly install without messing with permissions:

Sindre Sorus 有一个指南Install npm packagesglobal without sudo on OS X and Linux概述了如何干净地安装而不影响权限:

Here is a way to install packages globally for a given user.

  1. Create a directory for your global packages

    mkdir "${HOME}/.npm-packages"
    
  2. Reference this directory for future usage in your .bashrc/.zshrc:

    NPM_PACKAGES="${HOME}/.npm-packages"
    
  3. Indicate to npm where to store your globally installed package. In your $HOME/.npmrcfile add:

    prefix=${HOME}/.npm-packages
    
  4. Ensure node will find them. Add the following to your .bashrc/.zshrc:

    NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
    
  5. Ensure you'll find installed binaries and man pages. Add the following to your .bashrc/.zshrc:

    PATH="$NPM_PACKAGES/bin:$PATH"
    # Unset manpath so we can inherit from /etc/manpath via the `manpath`
    # command
    unset MANPATH # delete if you already modified MANPATH elsewhere in your config
    MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
    

Check out npm-g_nosudofor doing the above steps automagically

这是一种为给定用户全局安装软件包的方法。

  1. 为您的全局包创建一个目录

    mkdir "${HOME}/.npm-packages"
    
  2. 在 .bashrc/.zshrc 中引用此目录以供将来使用:

    NPM_PACKAGES="${HOME}/.npm-packages"
    
  3. 指示 npm 在哪里存储全局安装的包。在您的$HOME/.npmrc文件中添加:

    prefix=${HOME}/.npm-packages
    
  4. 确保节点会找到它们。将以下内容添加到您的 .bashrc/.zshrc:

    NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
    
  5. 确保您会找到已安装的二进制文件和手册页。将以下内容添加到您的.bashrc/ .zshrc

    PATH="$NPM_PACKAGES/bin:$PATH"
    # Unset manpath so we can inherit from /etc/manpath via the `manpath`
    # command
    unset MANPATH # delete if you already modified MANPATH elsewhere in your config
    MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
    

查看npm-g_nosudo自动执行上述步骤

Checkout the source of this guidefor the latest updates.

查看本指南来源以获取最新更新。

回答by rich remer

TL;DR

TL; 博士

always use sudo -ior sudo -Hwhen running npm installto install global packages.

始终使用sudo -isudo -H运行npm install时安装全局包。

--

——

When you use npmit downloads packages to your user home directory. When you run as sudo, npminstalls files to the same directory, but now they are owned by root.

当您使用npm它时,它会将软件包下载到您的用户主目录。当您以 sudo 身份运行时,npm会将文件安装到同一目录,但现在它们归 root 所有。

So this is what happens to absolutely every single person who has ever used npm:

所以这绝对是每个曾经使用过的人都会发生的事情npm

  • install some local packages without issue using npm install foo
  • install global package using sudo install -g foo-cliwithout issue
  • attempt to install local package with npm install bar
  • get frustrated at the npmdesigners now that you have to go chmoda directory again
  • 使用安装一些本地包没有问题 npm install foo
  • 使用sudo install -g foo-cli没有问题的安装全局包
  • 尝试安装本地包 npm install bar
  • npm设计师感到沮丧,因为您必须chmod再次访问目录

When you use the -ior -Hoption with sudo, your home directory will be root's home directory. Any global installs will cache packages to /root/.npminstead of root-owned files at /home/me/.npm.

当您将-ior-H选项与 sudo 一起使用时,您的主目录将是root的主目录。任何全球安装将缓存的软件包/root/.npm,而不是root在自备的文件/home/me/.npm

Just always use sudo -ior sudo -Hwhen running npm installto install global packages and your npmpermissions problems will melt away.

只要始终使用sudo -isudo -H运行npm install时安装全局包,您的npm权限问题就会消失。

For good.

好的。

http://hood.ie/blog/why-you-shouldnt-use-sudo-with-npm.html

http://hood.ie/blog/why-you-shouldnt-use-sudo-with-npm.html

-- q.v.the accepted answer for fixing an already fwored npm.

- qv已接受的修复已被性交的答案npm