node.js 错误:EACCES:权限被拒绝,访问“/usr/local/lib/node_modules”反应

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

Error: EACCES: permission denied, access '/usr/local/lib/node_modules' react

node.jsreactjsnpminstall

提问by hendra dedi

What might be causing the error Error: EACCES: permission denied, access '/usr/local/lib/node_modules'?

什么可能导致错误Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/macbookmd101/.npm/_logs/2018-02-21T16_26_08_421Z-debug.log

回答by okandas

change your file permissions .. like this

更改您的文件权限.. 像这样

first check who owns the directory

首先检查谁拥有目录

ls -la /usr/local/lib/node_modules

it is denying access because the node_module folder is owned by root

它拒绝访问,因为 node_module 文件夹归 root 所有

drwxr-xr-x   3 root    wheel  102 Jun 24 23:24 node_modules

so this needs to be changed by changing root to your user but first run command below to check your current user How do I get the name of the active user via the command line in OS X?

所以这需要通过将 root 更改为您的用户来更改,但首先运行下面的命令来检查您当前的用户 如何在 OS X 中通过命令行获取活动用户的名称?

id -un

OR

或者

whoami

whoami

then change owner

然后更改所有者

sudo chown -R [owner]:[owner] /usr/local/lib/node_modules

OR

或者

sudo chown -R ownerName: /usr/local/lib/node_modules

OR

或者

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

回答by Naveen Raju

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use a hidden directory in your home directory.

为了最大限度地减少权限错误的可能性,您可以将 npm 配置为使用不同的目录。在本例中,您将在主目录中创建并使用一个隐藏目录。

Back up your computer. On the command line, in your home directory, create a directory for global installations:

备份您的计算机。在命令行上,在您的主目录中,为全局安装创建一个目录:

mkdir ~/.npm-global

mkdir ~/.npm-global

Configure npm to use the new directory path:

配置 npm 以使用新的目录路径:

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

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

In your preferred text editor, open or create a

在您首选的文本编辑器中,打开或创建一个

~/.profile

~/.profile

file and add this line:

文件并添加这一行:

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

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

On the command line, update your system variables:

在命令行上,更新您的系统变量:

source ~/.profile

source ~/.profile

To test your new configuration, install a package globally without using sudo

要测试您的新配置,请在不使用 sudo 的情况下全局安装软件包

回答by Nizar

All you need to do is to add USER to the owner of /local/lib

您需要做的就是将 USER 添加到 /local/lib 的所有者

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

回答by Pradeep Kumar HK

try appending sudobefore whatever command you are trying.

尝试在您尝试的任何命令之前附加sudo

like this : sudo npm install

像这样:sudo npm install

Using sudo with a command in Linux/UNIX generally elevates your permissions to superuser levels. In Windows, the superuser account is usually called 'Administrator.' In Linux/Unix the superuser account is generally named 'root'.

在 Linux/UNIX 中将 sudo 与命令一起使用通常会将您的权限提升到超级用户级别。在 Windows 中,超级用户帐户通常称为“管理员”。在 Linux/Unix 中,超级用户帐户通常被命名为“root”。

The root user has permission to access, modify or delete almost any file on your computer. Normal user accounts can access, modify or delete many fewer files. The restrictions on a normal account protect your computer from unauthorized or harmful programs or users. Some processes require you to perform actions on files or folders you don't normally have permissions to access. Installing a program that everyone can access is one of these actions.

root 用户有权访问、修改或删除您计算机上的几乎任何文件。普通用户帐户可以访问、修改或删除更少的文件。对普通帐户的限制可保护您的计算机免受未经授权或有害的程序或用户的侵害。某些进程要求您对通常无权访问的文件或文件夹执行操作。安装每个人都可以访问的程序是这些操作之一。

In your case, running the installation command with sudo gives you the permissions of the superuser, and allows you to modify files that your normal user doesn't have permission to modify.

在您的情况下,使用 sudo 运行安装命令将为您提供超级用户的权限,并允许您修改普通用户无权修改的文件。

回答by Quentin

I tried the solution of the answer given by @okanda but it didn't work for me.

我尝试了@okanda 给出的答案的解决方案,但它对我不起作用。

However it worked perfectly when I did it for several folders like mentioned in this thread: https://github.com/angular/angular-cli/issues/9676#issuecomment-464857493

但是,当我为这个线程中提到的几个文件夹执行此操作时,它运行良好:https: //github.com/angular/angular-cli/issues/9676#issuecomment-464857493

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

回答by cinobili19

While installing global packages in ubuntu, you need special write permissions as you are writing to the usr/binfolder. It is for security reasons. So, everytime you install a global package, use:

ubuntu 中安装全局包时,在写入usr/bin文件夹时需要特殊的写入权限。这是出于安全原因。因此,每次安装全局包时,请使用:

sudo npm install -g [package-name]

For your specific case, it will be:

对于您的具体情况,它将是:

sudo npm install -g typescript

回答by Suhas Gavad

It looks like you're running into permission issues. If you are installing npm-packagesthen it might possible that you are getting an EACCESerror when trying to install a package globally. This means you do not have permission to write to the directories npmuses to store global packages and commands.

看起来您遇到了权限问题。如果您正在安装,npm-packages那么EACCES在尝试全局安装软件包时可能会出现错误。这意味着您无权写入npm用于存储全局包和命令的目录。

Try running commands: sudo chmod u+x -R 775 ~/.npmand sudo chown $USER -R ~/.npmor you can just run any npmcommand with sudo, that should get resolve your issue.

尝试运行命令:sudo chmod u+x -R 775 ~/.npmsudo chown $USER -R ~/.npm或者你可以运行任何npm与命令sudo,应该得到解决您的问题。

If you are installing an npm-packagelocally, then you should be in your local project directory and can try running sudo npm install <pkg-name>command to install required package. the purpose of using sudois that it will change your owner permissions so you can make your current user authorized to run npmcommands.

如果你在npm-package本地安装,那么你应该在你本地的项目目录中,并且可以尝试运行sudo npm install <pkg-name>命令来安装所需的包。使用的目的sudo是它会更改您的所有者权限,以便您可以授权当前用户运行npm命令。

I'd recommend you to take a look at https://docs.npmjs.com/getting-started/fixing-npm-permissions

我建议您查看https://docs.npmjs.com/getting-started/fixing-npm-permissions

回答by Furquan

I was trying to install react expo and apart from sudo I had to add --unsafe-perm

我试图安装 react expo,除了 sudo 我不得不添加 --unsafe-perm

like this. This resolves my Issue

像这样。这解决了我的问题

sudo npm install -g expo-cli --unsafe-perm

回答by adnan javed

If you are facing this issue on you Mac. Follow these steps

如果您在 Mac 上遇到此问题。按着这些次序

First checking who is owner of this file by using below command

首先使用以下命令检查谁是此文件的所有者

  ls -la /usr/local/lib/node_modules
  ls -la /usr/local/lib/node_modules

you will find some file like below one of them is below

你会发现一些像下面这样的文件,其中之一在下面

drwxr-xr-x 3 root wheel 768 May 29 02:21 node_modules

drwxr-xr-x 3 根轮 768 May 29 02:21 node_modules

have you notice that above file is own by root, for make changes inside for you need to change owner ship of path.

你有没有注意到上面的文件是 root 拥有的,为了在里面进行更改,你需要更改路径的所有权。

you can use check who is current user by this command

您可以通过此命令检查谁是当前用户

  id -un                    (in my case user is yamsol)
  id -un                    (in my case user is yamsol)

and then you can change by calling this command (just replace your user name with ownerName)

然后您可以通过调用此命令进行更改(只需将您的用户名替换为 ownerName)

  sudo chown -R ownerName: /usr/local/lib/node_modules
  sudo chown -R ownerName: /usr/local/lib/node_modules

in my case as you know user is "yamsol" i will call this command in this way

在我的例子中,你知道用户是“yamsol”,我会以这种方式调用这个命令

  sudo chown -R yamsol: /usr/local/lib/node_modules
  sudo chown -R yamsol: /usr/local/lib/node_modules

thats it.

就是这样。

回答by angelcervera

Be careful with all responses that change the owner of all directories under /usr/localBasically, don't mess the linux system!!!

小心所有改变/usr/local下所有目录所有者的响应 基本上,不要搞砸 linux 系统!!!

I think that the best way is to use your own folder to locate all global packages: https://www.competa.com/blog/how-to-run-npm-without-sudo/

我认为最好的方法是使用自己的文件夹来定位所有全局包:https: //www.competa.com/blog/how-to-run-npm-without-sudo/