You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 23:07:31