React Native初始化项目失败:缺失template.config.js文件
React Native初始化报错:找不到template.config.js的解决方法
问题场景
执行初始化命令:
npx react-native@latest init AwesomeProject
出现如下报错:
✔ Downloading template ✖ Copying template error Error: Couldn't find the "/var/folders/8x/y10dxy554tzb5d5d2tv_rffh0000gn/T/rncli-init-template-c131XZ/node_modules/react-native/template.config.js file inside "react-native" template. Please make sure the template is valid. Read more: https://github.com/react-native-community/cli/blob/master/docs/init.md#creating-custom-template. Error: Error: Couldn't find the "/var/folders/8x/y10dxy554tzb5d5d2tv_rffh0000gn/T/rncli-init-template-c131XZ/node_modules/react-native/template.config.js file inside "react-native" template. Please make sure the template is valid. Read more: https://github.com/react-native-community/cli/blob/master/docs/init.md#creating-custom-template at createFromTemplate (/Users/rmunoz/.npm/_npx/dea611a43221eddd/node_modules/@react-native-community/cli/build/commands/init/init.js:131:11) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Object.initialize [as func] (/Users/rmunoz/.npm/_npx/dea611a43221eddd/node_modules/@react-native-community/cli/build/commands/init/init.js:184:3) at async Command.handleAction (/Users/rmunoz/.npm/_npx/dea611a43221eddd/node_modules/@react-native-community/cli/build/index.js:106:9) info Run CLI with --verbose flag for more details.
已确认对应目录下template.config.js不存在,不想创建自定义模板,且已安装Xcode CLI工具和Ruby 2.7.6,需要解决该初始化错误。
解决方法
清理缓存与临时文件
删除初始化生成的临时目录:/var/folders/8x/y10dxy554tzb5d5d2tv_rffh0000gn/T/rncli-init-template-c131XZ,再清理npm缓存:npm cache clean --force完成后重新执行初始化命令。
指定稳定版本初始化
避免使用@latest拉取可能存在问题的最新版本,改用官方稳定版本(例如0.72.6):npx react-native@0.72.6 init AwesomeProject检查Node.js版本兼容性
确保Node.js版本符合React Native官方要求(推荐LTS版本如18.x),查看版本:node -v npm -v若版本不符,升级或降级后重试。
手动创建缺失的配置文件
进入临时目录的react-native文件夹:cd /var/folders/8x/y10dxy554tzb5d5d2tv_rffh0000gn/T/rncli-init-template-c131XZ/node_modules/react-native创建
template.config.js并写入基础配置:module.exports = { name: 'react-native-template', templateDir: './template', placeholderName: 'HelloWorld', };保存后回到项目目录重新执行初始化命令。
内容的提问来源于stack exchange,提问作者Rodrigo Munoz
相关产品推荐
相关产品推荐

