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

Create React App运行Jest报transform must export `process`函数错误如何解决

报错根因

你使用的react-scripts@1.1.4是早期CRA版本,内置Jest版本为22.x。当前报错由版本不兼容导致:

  1. 直接配置"test": "jest"调用的Jest版本,与CRA内置的代码转换插件版本不匹配,转换插件未导出要求的process方法
  2. 高版本测试依赖@testing-library/react@12.x、@testing-library/jest-dom@5.x与低版本Jest运行环境不兼容
修复步骤
  • 修改package.json的scripts配置,优先使用CRA内置的测试运行命令,避免版本冲突
    将原test命令替换为:
"test": "react-scripts test --env=jsdom"

如果需要对接sonar报告,可保留jest-sonar-reporter配置:

"test": "react-scripts test --env=jsdom --testResultsProcessor jest-sonar-reporter"
  • 降级测试依赖到匹配Jest 22.x的版本
    执行以下命令卸载不兼容的高版本依赖,安装对应兼容版本:
npm uninstall @testing-library/react @testing-library/jest-dom
npm install --save-dev @testing-library/react@11.x @testing-library/jest-dom@4.x
  • 清除Jest缓存后重新运行测试
npx jest --clearCache
npm run test

若以上方案仍报错,说明项目内额外安装了不兼容的高版本Jest,可执行以下操作修复:

  1. 安装与内置Jest版本匹配的转换依赖:
npm install --save-dev babel-jest@22.x babel-core@^7.0.0-bridge.0
  1. 修改test命令调用项目内置Jest:
"test": "node_modules/.bin/jest"

内容的提问来源于stack exchange,提问作者Huzan Toorkey

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 18:09:03