运行npx create-react-app报错,全局安装遇EACCES权限问题求助
解决create-react-app全局安装的EACCES权限错误
环境信息
- Node.js版本:v16.14.2
- npm版本:v8.5.0
错误详情
执行npm install -g create-react-app时触发权限拒绝错误:
npm ERR! code EACCES npm ERR! syscall rename npm ERR! path /usr/local/lib/node_modules/create-react-app npm ERR! dest /usr/local/lib/node_modules/.create-react-app-DgI96EzL npm ERR! errno -13 npm ERR! 错误:EACCES:权限被拒绝,重命名 '/usr/local/lib/node_modules/create-react-app' -> '/usr/local/lib/node_modules/.create-react-app-DgI96EzL' npm ERR! [错误:EACCES:权限被拒绝,重命名 '/usr/local/lib/node_modules/create-react-app' -> '/usr/local/lib/node_modules/.create-react-app-DgI96EzL'] { npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! syscall: 'rename', npm ERR! path: '/usr/local/lib/node_modules/create-react-app', npm ERR! dest: '/usr/local/lib/node_modules/.create-react-app-DgI96EzL' npm ERR! } npm ERR! npm ERR! 操作被操作系统拒绝。 npm ERR! 很可能当前用户没有访问该文件的权限。 npm ERR! npm ERR! 如果您认为这可能是权限问题,请仔细检查文件及其所在目录的权限,或尝试以root/管理员身份重新运行该命令。 npm ERR! 本次运行的完整日志可在以下位置查看: npm ERR! /Users/kiranrana/.npm/_logs/2022-07-12T12_00_07_463Z-debug-0.log
解决方法
方法1:临时使用管理员权限安装
直接用sudo提升权限执行安装命令:
sudo npm install -g create-react-app
输入系统用户密码后即可完成安装,注意后续尽量避免频繁使用sudo操作npm,防止权限混乱。
方法2:修改npm全局目录权限(推荐长期方案)
通过配置npm使用用户主目录下的全局文件夹,避免权限问题:
- 创建自定义全局npm目录
mkdir ~/.npm-global - 配置npm指向该目录
npm config set prefix '~/.npm-global' - 更新shell配置文件(根据终端类型选择)
- 若使用bash,编辑
~/.bash_profile或~/.bashrc,添加:export PATH=~/.npm-global/bin:$PATH - 若使用zsh,编辑
~/.zshrc,添加同样的PATH配置
- 若使用bash,编辑
- 使配置立即生效
source ~/.bash_profile # bash终端 # 或 source ~/.zshrc # zsh终端 - 正常安装create-react-app
npm install -g create-react-app
方法3:直接使用npx创建项目(官方推荐)
官方已不建议全局安装create-react-app,直接用npx命令即可创建项目,无需全局安装,自动使用最新版本:
npx create-react-app your-app-name
将your-app-name替换为你的项目名称即可。
内容的提问来源于stack exchange,提问作者Kiran Rana
相关产品推荐
相关产品推荐

