npm install -g yarn 报错 -13 code: ‘EACCES‘ 亲测有效

在配置新mac的环境的时候,发现yarn无论如何也无法下载安装,搜了很久,最终找到了一个完美的结局办法,亲测有效

报错内容

zhaowei@zhaoweideMacBook-Pro ~ % npm install -g yarn
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/yarn
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/yarn'
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/zhaowei/.npm/_logs/2022-11-18T02_54_56_788Z-debug-0.log

原因分析-权限问题

解决方案

输入

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

输入密码

zhaowei@zhaoweideMacBook-Pro ~ % sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Password:

然后正常安装 npm isntall -g yarn

可能会提示npm版本低 升级即可

zhaowei@zhaoweideMacBook-Pro ~ % npm install -g yarn      

added 1 package, and audited 2 packages in 3s

found 0 vulnerabilities
npm notice 
npm notice New major version of npm available! 8.5.0 -> 9.1.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.1.2
npm notice Run npm install -g [email protected] to update!
npm notice 
zhaowei@zhaoweideMacBook-Pro ~ % npm install -g [email protected]

removed 31 packages, changed 96 packages, and audited 223 packages in 3s

14 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

升级完后重新安装yarn 并检测是否安装成功

zhaowei@zhaoweideMacBook-Pro ~ % npm install -g yarn     

changed 1 package in 686ms
zhaowei@zhaoweideMacBook-Pro ~ % yarn -v
1.22.19

完美解决

你可能感兴趣的:(入门分享,npm,前端,node.js)