是什么原因导致错误:EACCES:权限被拒绝,访问'/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

当前回答

如果你正在运行Linux(如:Arch)

# npm -g install packageName

默认情况下,该命令将包安装在/usr/lib/node_modules/npm下,并且需要root权限才能这样做。

允许用户范围的安装

要允许为当前用户安装全局包,请设置npm_config_prefix环境变量。npm和yarn都使用这个。

mkdir ~/.node_modules
cat ~/.profile
PATH="$HOME/.node_modules/bin:$PATH"
export npm_config_prefix=~/.node_modules

重新登录或更新源代码以更新更改。

你也可以为npm install指定——prefix参数。但是,不建议这样做,因为每次安装全局包时都需要添加它。

$ npm -g install packageName --prefix ~/.node_modules

所以你不需要chown文件夹权限

其他回答

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

尝试执行命令:sudo chmod u+x -R 775 ~/。npm和sudo chown $USER -R ~/。或者你可以用sudo运行任何NPM命令,这应该能解决你的问题。

如果你在本地安装npm-package,那么你应该在你的本地项目目录下,可以尝试运行sudo npm install <pkg-name>命令来安装所需的包。使用sudo的目的是改变你的所有者权限,这样你就可以授权你的当前用户运行NPM命令。

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

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

像这样:sudo NPM install

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

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

在本例中,使用sudo运行安装命令可以为您提供超级用户的权限,并允许您修改普通用户没有权限修改的文件。

只要在npm命令前加上“sudo”即可。这是它。

对于linux / ubuntu如果命令

NPM install -g <package_name>

npm WARN deprecated superagent@4.1.0: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header).  This notice will go away with v5.0.2+ once it is released.

npm ERR! path ../lib/node_modules/<package_name>/bin/..

npm ERR! code EACCES

npm ERR! errno -13

npm ERR! syscall symlink

npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules
/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'

npm ERR!  { [Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>']

npm ERR!   cause:
npm ERR!    { Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'

npm ERR!      errno: -13,

npm ERR!      code: 'EACCES',

npm ERR!      syscall: 'symlink',

npm ERR!      path: '../lib/node_modules/<package_name>/bin/..',
npm ERR!      dest: '/usr/local/bin/ionic' },

npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, symlink \'../lib/node_modules/ionic/bin/ionic\' -> \'/usr/local/bin/ionic\'',

npm ERR!   errno: -13,

npm ERR!   code: 'EACCES',

npm ERR!   syscall: 'symlink',

npm ERR!   path: '../lib/node_modules/<package-name>/bin/<package-name>',

npm ERR!   dest: '/usr/local/bin/<package-name>' }

npm ERR! 

npm ERR! The operation was rejected by your operating system.

npm ERR! It is likely you do not have the permissions to access this file as the current user

npm ERR! 

npm ERR! If you believe this might be a permissions issue, please double-check the

npm ERR! permissions of the file and its containing directories, or try running

npm ERR! the command again as root/Administrator (though this is not recommended).


npm ERR! A complete log of this run can be found in:

npm ERR!     /home/User/.npm/_logs/2019-07-29T01_20_10_566Z-debug.log

修复:安装与根权限

Sudo NPM install <package_name> -g

我得到了同样的错误,并解决它使用命令

Sudo NPM install -g expo-cli——unsafe-perm

在运行命令后的图像中

然后执行以下命令更新NPM

NPM install -g npm@latest

我很容易就解决了这个问题。