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

React项目npm run start正常但build:production报constantViolations未定义错误求助

React生产构建失败(@floating-ui/core抛出TypeError)

问题描述

使用React.js开发项目,npm run start可正常启动开发环境,但执行npm run build:production时构建失败,报错信息如下:

TypeError: E:\temp\webapp-newui-webapp-winterupdate\node_modules\@floating-ui\core\dist\floating-ui.core.browser.min.mjs: Cannot read properties of undefined (reading 'constantViolations')
    at transformFile.next (<anonymous>)
    at run.next (<anonymous>)
    at transform.next (<anonymous>)

项目的package.json脚本配置:

{
    "name": "webApp",
    "version": "0.0.1",
    "description": "React Crypto Exchange setup",
    "scripts": {
        "start": "env-cmd -f .env.development react-app-rewired start",
        "start:testnet": "env-cmd -f .env.testnet react-app-rewired start",
        "dev": "env-cmd -f .env.local react-app-rewired start",
        "start:prod": "env-cmd -f .env.production react-app-rewired start",
        "build:production": "env-cmd -f .env.production react-app-rewired build&&cp build/index.html build/200.html",
        "build:development": "env-cmd -f .env.development react-app-rewired build&&cp build/index.html build/200.html",
        "build:testnet": "env-cmd -f .env.development react-app-rewired build&&cp build/index.html build/200.html",
        "build:surge": "env-cmd -f .env.development react-app-rewired build&&cp build/index.html build/200.html&& surge --project ./build --domain https://fanfury-test.surge.sh",
    },
    ...
}

解决建议

  • 锁定@floating-ui稳定版本
    该错误通常是@floating-ui版本与项目依赖(如react-app-rewired、Babel)不兼容导致的。尝试降级到经过验证的稳定版本:

    npm uninstall @floating-ui/core @floating-ui/react-dom
    npm install @floating-ui/core@^1.6.0 @floating-ui/react-dom@^1.6.0
    

    之后重新执行构建命令。

  • 检查react-app-rewired配置
    打开config-overrides.js,确认是否有修改Babel或Webpack的生产环境配置。比如是否禁用了某些ES模块转译插件,或者修改了预设规则,导致@floating-ui的模块无法被正确转译。确保生产环境的转译配置包含对node_modules/@floating-ui的正确处理。

  • 清理依赖与构建缓存
    缓存损坏也可能引发这类问题,执行以下步骤重置依赖:

    npm cache clean --force
    rm -rf node_modules package-lock.json # Windows环境执行:del /q node_modules && del package-lock.json
    npm install
    

    清理完成后再次尝试构建。

  • 拆分构建命令排查
    暂时去掉构建命令后的cp操作,单独执行:

    env-cmd -f .env.production react-app-rewired build
    

    确认是否是构建本身出错,而非后续的文件复制步骤影响。

  • 验证生产环境变量
    对比.env.production和.env.development的变量,确保所有必要的环境变量都已定义且格式正确。虽然start:prod能运行,但构建过程中某些变量缺失可能间接导致依赖库报错。

内容的提问来源于stack exchange,提问作者Bikas Lin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 03:35:17