macOS Ventura 13.2创建React Native项目遇模板复制错误求助
问题重现
在macOS Ventura 13.2中执行React Native项目初始化时,模板下载成功但复制失败,报错如下:
✔ Downloading template
✖ Copying template
error Error: Couldn't find the "/var/folders/mn/m8xhgjss2cj0jf5svmxg_2vm0000gn/T/rncli-init-template-VUPUIq/node_modules/react-native/template.config.js file inside "react-native" template. Please make sure the template is valid.
Error: Error: Couldn't find the "/var/folders/mn/m8xhgjss2cj0jf5svmxg_2vm0000gn/T/rncli-init-template-VUPUIq/node_modules/react-native/template.config.js file inside "react-native" template. Please make sure the template is valid.
at createFromTemplate (/Users/imac/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/init/init.js:129:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.initialize [as func] (/Users/imac/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/init/init.js:181:3)
at async Command.handleAction (/Users/imac/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/index.js:106:9)
info Run CLI with --verbose flag for more details.
已尝试全局卸载重装react-native-cli、@react-native-community/cli及Node.js,问题仍存在。
可行解决方案
1. 清理临时文件与缓存
- 手动删除报错中指向的临时模板目录:
rm -rf /var/folders/mn/m8xhgjss2cj0jf5svmxg_2vm0000gn/T/rncli-init-template-VUPUIq - 清理npm缓存:
npm cache clean --force - 清理React Native CLI缓存:
npx react-native clean
2. 指定稳定版本初始化
最新版本模板可能存在兼容性问题,指定一个经过验证的稳定版本创建项目:
npx react-native init MyRNApp --version 0.72.6
3. 切换至官方npm镜像
国内镜像可能导致模板文件下载不完整,切换回官方镜像后重试:
npm config set registry https://registry.npmjs.org/ npx react-native init MyRNApp
4. 手动指定本地模板路径
- 从React Native官方仓库下载对应版本的模板包
- 解压到本地目录,执行初始化时指定模板路径:
npx react-native init MyRNApp --template file:///Users/yourname/Downloads/react-native-template
5. 检查系统权限
macOS临时目录权限异常可能导致文件读取失败,可尝试以管理员权限执行初始化:
sudo npx react-native init MyRNApp
(注意:使用sudo后需确保项目目录权限正确,后续避免频繁用sudo操作npm)
内容的提问来源于stack exchange,提问作者Rabi3 Fegga

