报错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:
虽然像这种权限问题加个sudo运行就可以顺利安装,但这绝非一个好的方法,因为当我之前毫不犹豫这样解决的时候遇到了很多问题。
比如每次像这样的全局安装卸载等操作时都需要sudo
其次因为这个文件是sudo安装的,因此文件权限就是root,之后对这个文件的操作更是会面临诸多问题。
因此我尝试了新的解决方案,创建一个用户有权限的global目录。具体命令如下:
cd ~
mkdir .npm-global
npm config set prefix '~/.npm-global'
更新path
export PATH="~/.npm-global/bin:$PATH"
至此完美解决,此后不需要在每次全局安装都sudo
具体原理是切到用户目录创建用户有权限的全局文件,
将安装prefix目录设置为该目录。当不设置,将默认安装至node安装的目录,最后更新path
相关文档具体可参考: