BitBucket构建容器内存超限,React生产部署失败求助
问题:React生产构建因内存不足失败
执行yarn build时返回如下错误:
yarn build yarn run v1.22.19 $ react-app-rewired build Creating an optimized production build... The build failed because the process exited too early. This probably means the system ran out of memory or someone called `kill -9` on the process. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. error Command failed with exit code 1.
BitBucket Pipeline配置文件如下:
image: atlassian/default-image:3 pipelines: branches: stage: #STAGE - step: name: Build docker deployment: Staging services: - docker image: node:16-alpine size: 2x script: - export NODE_OPTIONS=--max-old-space-size=8192 - yarn install - yarn build - echo $REACT_APP_AUTH - echo $REACT_APP_MEMBERSHIP - docker login -u $DOCKER_HUB_ID -p $DOCKER_HUB_PASSWORD - docker build -t evoeccdocker/membership_front:stage --build-arg REACT_APP_AUTH=$REACT_APP_AUTH --build-arg REACT_APP_MEMBERSHIP=$REACT_APP_MEMBERSHIP --build-arg REACT_APP_PAYMENT=$REACT_APP_PAYMENT --build-arg REACT_APP_APPLICATION_ID=$REACT_APP_APPLICATION_ID --build-arg REACT_APP_APPLICATION_KEY=$REACT_APP_APPLICATION_KEY --build-arg REACT_APP_TINYMCE=$REACT_APP_TINYMCE --build-arg REACT_APP_GOOGLE_CLIENT_ID=$REACT_APP_GOOGLE_CLIENT_ID --build-arg REACT_APP_GOOGLE_ANALYTICS=$REACT_APP_GOOGLE_ANALYTICS --build-arg REACT_APP_PAYMENT_PAGE_STORAGE_ENCRYPT_SALT=$REACT_APP_PAYMENT_PAGE_STORAGE_ENCRYPT_SALT --build-arg REACT_APP_PAYPAL_CLIENT_ID=$REACT_APP_PAYPAL_CLIENT_ID -f ./Dockerfile-stage . - docker push evoeccdocker/membership_front:stage caches: - docker - nodecustom - yarn master: #MASTER - step: name: Build docker image: node:16-alpine size: 2x deployment: Production services: - docker script: - export NODE_OPTIONS=--max-old-space-size=8192 - yarn install - yarn build - docker login -u $DOCKER_HUB_ID -p $DOCKER_HUB_PASSWORD - docker build -t evoeccdocker/membership_front:prod --build-arg REACT_APP_AUTH=$REACT_APP_AUTH --build-arg REACT_APP_MEMBERSHIP=$REACT_APP_MEMBERSHIP --build-arg REACT_APP_PAYMENT=$REACT_APP_PAYMENT --build-arg REACT_APP_APPLICATION_ID=$REACT_APP_APPLICATION_ID --build-arg REACT_APP_APPLICATION_KEY=$REACT_APP_APPLICATION_KEY --build-arg REACT_APP_TINYMCE=$REACT_APP_TINYMCE --build-arg REACT_APP_GOOGLE_CLIENT_ID=$REACT_APP_GOOGLE_CLIENT_ID --build-arg REACT_APP_GOOGLE_ANALYTICS=$REACT_APP_GOOGLE_ANALYTICS --build-arg REACT_APP_PAYMENT_PAGE_STORAGE_ENCRYPT_SALT=$REACT_APP_PAYMENT_PAGE_STORAGE_ENCRYPT_SALT --build-arg REACT_APP_PAYPAL_CLIENT_ID=$REACT_APP_PAYPAL_CLIENT_ID -f ./Dockerfile-prod . - docker push evoeccdocker/membership_front:prod caches: - docker - nodecustom - yarn definitions: caches: nodecustom: ./node_modules yarn: /usr/local/share/.cache/yarn
解决与排查方案
一、即时解决措施
- 调整Node内存限制的生效方式:将内存参数直接绑定到build命令,避免环境变量传递失效。修改
package.json的scripts:"scripts": { "build": "react-app-rewired --max-old-space-size=8192 build" } - 升级Pipeline资源规格:当前使用
size:2x,若BitBucket允许,尝试切换到size:3x,为构建过程分配更多内存。 - 清理缓存后重建:缓存可能引发依赖异常,在Pipeline的script中添加清理步骤:
- yarn cache clean - rm -rf node_modules - yarn install --force
二、排查内存占用峰值根源
- 启用构建时内存监控:添加GC追踪日志,观察内存变化:
日志会显示垃圾回收频率和内存释放情况,可定位内存暴增的阶段。cross-env NODE_OPTIONS="--max-old-space-size=8192 --trace-gc" yarn build - 逐步排查第三方依赖/插件:
- 临时替换
react-app-rewired为原生react-scripts build,若构建成功,说明自定义配置中的插件存在问题。 - 逐个移除
config-overrides.js中的插件,每次构建验证是否还会内存溢出,定位具体问题插件。
- 临时替换
- 分析Bundle体积:用
source-map-explorer找出大体积依赖,考虑替换或按需引入:yarn add source-map-explorer --dev yarn build npx source-map-explorer build/static/js/*.js - 检查代码冗余:清理未使用的组件、重复导入的大型库,将静态资源改为CDN引入,减少构建时的内存压力。
内容的提问来源于stack exchange,提问作者Fernanda
相关产品推荐
相关产品推荐

