创建React项目时遇EPERM权限错误,求解决方案
解决npx create-react-app的EPERM权限错误
问题背景
执行npx create-react-app crud-book-management时遭遇EPERM权限错误,具体报错如下:
npm ERR! code EPERM npm ERR! syscall symlink npm ERR! path ../@babel/parser/bin/babel-parser.js npm ERR! dest /media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management/crud-boo-management/node_modules/.bin/parser npm ERR! errno -1 npm ERR! Error: EPERM: operation not permitted, symlink '../@babel/parser/bin/babel-parser.js' -> '/media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management/crud-boo-management/node_modules/.bin/parser' npm ERR! [Error: EPERM: operation not permitted, symlink '../@babel/parser/bin/babel-parser.js' -> '/media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management/crud-boo-management/node_modules/.bin/parser'] { npm ERR! errno: -1, npm ERR! code: 'EPERM', npm ERR! syscall: 'symlink', npm ERR! path: '../@babel/parser/bin/babel-parser.js', npm ERR! dest: '/media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management/crud-boo-management/node_modules/.bin/parser' 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! /home/marcius/.npm/_logs/2022-08-31T15_45_59_824Z-debug-0.log
尝试用sudo执行时提示npm命令未找到,且不希望通过sudo创建项目。
已尝试操作及结果
- 执行
sudo npm install -g create-react-app,安装成功但问题依旧:npm WARN deprecated tar@2.2.2: This version of tar is no longer supported, and will not receive security updates. Please upgrade asap. added 67 packages, and audited 68 packages in 2s 4 packages are looking for funding run `npm fund` for details 2 high severity vulnerabilities Some issues need review, and may require choosing a different dependency. Run `npm audit` for details. - 添加
--no-bin-links参数执行,安装失败:Creating a new React app in /media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management/crud-book-management. Installing packages. This might take a couple of minutes. Installing react, react-dom, and react-scripts... Aborting installation. npm install --no-audit --save --save-exact --loglevel error react react-dom react-scripts@0.9.x has failed. Deleting generated file... package.json Deleting crud-book-management/ from /media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management Done. - 尝试sudo执行依赖安装命令,仍触发EPERM错误:
npm ERR! code EPERM npm ERR! syscall symlink npm ERR! path ../acorn/bin/acorn npm ERR! dest /media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management/node_modules/.bin/acorn npm ERR! errno -1 npm ERR! Error: EPERM: operation not permitted, symlink '../acorn/bin/acorn' -> '/media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management/node_modules/.bin/acorn' npm ERR! [Error: EPERM: operation not permitted, symlink '../acorn/bin/acorn' -> '/media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management/node_modules/.bin/acorn'] { npm ERR! errno: -1, npm ERR! code: 'EPERM', npm ERR! syscall: 'symlink', npm ERR! path: '../acorn/bin/acorn', npm ERR! dest: '/media/marcius/GERAL/SENAC/PSEL/Aula 00 - CRUD em React/crud-book-management/node_modules/.bin/acorn' 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! /root/.npm/_logs/2022-08-31T16_49_12_715Z-debug-0.log
问题根源与解决方案
核心原因
你的项目目录位于挂载的外部存储设备(/media/marcius/GERAL/...),这类设备默认不允许创建符号链接(symlink),而npm安装依赖时必须创建符号链接,这是触发EPERM错误的关键。另外,sudo找不到npm是因为root用户的PATH环境变量未包含npm的安装路径。
解决方法
1. 切换到本地磁盘创建项目(推荐)
将项目创建在本地用户目录下(比如/home/marcius/),本地磁盘默认支持符号链接,且你拥有完整权限:
# 创建本地项目目录 mkdir -p ~/react-projects # 进入目录 cd ~/react-projects # 创建React项目 npx create-react-app crud-book-management
2. 开启外部存储的符号链接支持(可选)
如果必须在外部存储上创建项目,需要重新挂载设备并启用符号链接:
- 先卸载设备:
sudo umount /media/marcius/GERAL - 重新挂载并添加符号链接支持参数:
注意替换sudo mount -t auto /dev/sdXn /media/marcius/GERAL -o defaults,acl,user_xattr,follow_symlinks/dev/sdXn为你的设备实际路径(可通过lsblk命令查看)。 - 若要永久生效,编辑
/etc/fstab文件,添加对应挂载项的参数:/dev/sdXn /media/marcius/GERAL auto defaults,acl,user_xattr,follow_symlinks 0 0
3. 用nvm管理Node.js避免权限问题
全局安装npm包时,使用nvm管理Node.js版本可以彻底避免sudo权限问题:
- 安装nvm后,重新打开终端,安装最新Node.js版本:
之后执行nvm install node nvm use nodenpx create-react-app就不会出现权限问题。
内容的提问来源于stack exchange,提问作者Marcius Leandro
相关产品推荐
相关产品推荐

