npm 命令报权限不足错误 Error EACCES permission denied

释放双眼,带上耳机,听听看~!

在使用 npm 命令安装组件的时候,经常会报 权限不足 的错误:Error: EACCES: permission denied ,虽然用 sudo 可以解决问题,但都只是暂时的;

错误信息

npm WARN checkPermissions Missing write access to /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!   stack:
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! 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:

解决方法

  1. 重装 version manager
  2. 改变 npm 的默认路径

这里我选择了第 2

中文译

创建global安装任务的目录

mkdir ~/.npm-global

配置npm使用新的目录

npm config set prefix '~/.npm-global'

在~/.profile文件中增加配置

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

配置文件立即生效

source ~/.profile

重新执行命令

npm install -g xxxx

人已赞赏