使用npm命令时报错误Error EACCES:permission denied,如何解决

2022/10/26 m1npmyarn

mac m1 全局安装yarnnpm install -g yarn 时提示permission denied权限不足试了几个方法 还是下面这个好用;

# 报错信息;

a9374@9374deMacBook-Pro happyboot-tiger % npm install -g yarn                    
npm ERR! code EACCES
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/yarn/bin/yarn.js
npm ERR! dest /usr/local/bin/yarnpkg
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/yarn/bin/yarn.js' -> '/usr/local/bin/yarnpkg'
npm ERR!  [Error: EACCES: permission denied, symlink '../lib/node_modules/yarn/bin/yarn.js' -> '/usr/local/bin/yarnpkg'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'symlink',
npm ERR!   path: '../lib/node_modules/yarn/bin/yarn.js',
npm ERR!   dest: '/usr/local/bin/yarnpkg'
npm ERR! }
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.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/a9374/.npm/_logs/2022-10-26T01_02_09_070Z-debug-0.log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 解决方法

官方的解决方案 (opens new window) 官方提供了两种解决方案:

  1. 重新安装一个node version manager。(官方推荐) 参考地址 (opens new window)
    注:这个方法比较繁琐,我选择的是第二种方案。
  2. 改变npm默认的路径。 进入终端,依次输入一下命令
  #(1)创建global安装任务的目录
  mkdir ~/.npm-global
  #(2)配置npm使用新的目录
  npm config set prefix '~/.npm-global'
  #(3)在~/.profile文件中增加配置
  export PATH=~/.npm-global/bin:$PATH
  #(4)配置文件立即生效
  source ~/.profile
  #(5)重新执行命令
  npm install -g xxxx 
1
2
3
4
5
6
7
8
9
10
最后更新时间: 2023/1/12 15:59:09