Error: EACCES: permission denied, mkdir ‘/usr/local/lib/node_modules/xxxxxx’

1 问题描述

今天使用 npm i docsify-cli -g 安装 node 脚手架工具时,出现以下错误:

andrew@Andrews-MacBook-Pro CSNotes % npm i docsify-cli -g
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/docsify-cli
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/docsify-cli'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/docsify-cli'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/docsify-cli'
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/andrew/.npm/_logs/2022-03-12T07_59_19_036Z-debug-0.log

看到 Error: EACCES: permission denied 这句话时,该问题很明显是用户权限不足了。

2 解决方案

解决该权限问题核心思想是提权,但有两种思路:

  • 对文件目录提权
 sudo chown -R $USER /usr/local/lib/node_modules
  • 对执行用户提权
sudo npm i docsify-cli -g

最终我们顺利安装成功:
在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐