当前位置:   article > 正文

彻底解决全局安装权限问题_npm err! code eacces npm err! syscall mkdir npm er

npm err! code eacces npm err! syscall mkdir npm err! path /usr/local/lib/nod

报错EACCES: permission denied,具体信息如下:

npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/@vue
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/@vue'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/@vue'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/@vue'
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:
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

虽然像这种权限问题加个sudo运行就可以顺利安装,但这绝非一个好的方法,因为当我之前毫不犹豫这样解决的时候遇到了很多问题。
比如每次像这样的全局安装卸载等操作时都需要sudo
其次因为这个文件是sudo安装的,因此文件权限就是root,之后对这个文件的操作更是会面临诸多问题。

因此我尝试了新的解决方案,创建一个用户有权限的global目录。具体命令如下:

cd ~
mkdir .npm-global
npm config set prefix '~/.npm-global'
  • 1
  • 2
  • 3

更新path

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

至此完美解决,此后不需要在每次全局安装都sudo

具体原理是切到用户目录创建用户有权限的全局文件,
将安装prefix目录设置为该目录。当不设置,将默认安装至node安装的目录,最后更新path

在这里插入图片描述

相关文档具体可参考:

  • https://www.npmjs.cn/files/folders/ (npm folder)
  • https://stackoverflow.com/questions/764600/how-can-you-export-your-bashrc-to-zshrc
  • https://www.cnblogs.com/tinywan/p/7224011.html
  • https://stackoverflow.com/questions/58269937/permission-denied-while-trying-to-install-vue-cli
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/333538
推荐阅读
相关标签
  

闽ICP备14008679号