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

React+Vite在AWS构建时出现exit status 137错误求助

AWS环境下React+Vite构建报错exit status 137的排查与解决

问题详情

本地执行npm run build构建React+Vite项目无异常,但在AWS环境中执行时触发exit status 137错误,报错信息如下:

vite build --out-dir build
The CJS build of Vite's Node API is deprecated. See相关文档了解详情。 vite v5.2.13 building for production...transforming...
✓ 5152 modules transformed. rendering chunks.../codebuild/output/tmp/script.sh: line 4: 178 Killed
npm run build [Container] 2024/06/26 11:34:48.511277 Command did not exit successfully npm run build exit status 137 [Container] 2024/06/26 11:34:48.574011 Phase complete: BUILD State: FAILED [Container]2024/06/26 11:34:48.574038 Phase context status code:COMMAND_EXECUTION_ERROR Message: Error while executing command: npm run build. Reason: exit status 137

已尝试设置NODE_OPTIONS='--max-old-space-size=2048'提升内存,但问题未解决。

当前vite.config中的build配置:

build: {
    chunkSizeWarningLimit: 40000,
    //minify: false,
    sourcemap: true,
    rollupOptions: {
        onLog(level, log, handler) {
            if (log.cause && log.cause.message === `Can't resolve original location of error.`) {
                return;
            }
            handler(level, log);
        },
    },
},

本地build脚本:

"build": "cross-env NODE_OPTIONS='--max-old-space-size=2048' vite build --out-dir build",


解决思路与实操方案

1. 升级AWS构建环境的内存配额

exit status 137本质是进程因内存不足被系统强制杀死。即使设置了Node内存上限,如果AWS构建容器的物理内存不足,依然会触发问题:

  • 检查AWS CodeBuild项目的计算类型配置,将默认的BUILD_GENERAL1_SMALL(1GB内存)升级为BUILD_GENERAL1_MEDIUM(3GB)或更高规格,确保容器有足够的物理内存分配给Node进程。

2. 优化Vite构建的内存占用

从构建配置入手降低内存消耗:

  • 关闭生产环境sourcemap:注释或修改sourcemap: true为sourcemap: false,生成sourcemap会大幅增加构建时的内存开销,若需调试可后续单独生成。
  • 启用代码压缩:取消注释minify: false,默认Vite使用esbuild压缩代码,未压缩的大体积代码会导致Rollup处理时内存飙升。
  • 拆分超大chunk:当前chunkSizeWarningLimit设为40MB,过大的chunk会加重内存负担,降低该值并配置Rollup自动拆分第三方依赖:
    rollupOptions: {
        output: {
            manualChunks(id) {
                // 将第三方依赖拆分为独立chunk
                if (id.includes('node_modules')) {
                    return id.split('node_modules/')[1].split('/')[0];
                }
            }
        },
        // 保留原有日志处理逻辑
        onLog(level, log, handler) {
            if (log.cause && log.cause.message === `Can't resolve original location of error.`) {
                return;
            }
            handler(level, log);
        },
    }
    

3. 修正NODE_OPTIONS的配置方式

AWS环境中可能存在环境变量解析问题,调整build脚本的写法:

  • 去掉变量值的引号,避免解析异常:
    "build": "cross-env NODE_OPTIONS=--max-old-space-size=3072 vite build --out-dir build"
    
  • 或者在AWS构建命令中直接前置环境变量设置:
    export NODE_OPTIONS="--max-old-space-size=3072" && npm run build
    

4. 清理构建缓存

AWS构建环境可能残留旧缓存导致内存占用异常,在构建前添加缓存清理步骤:

  • 修改build脚本,先清理Vite缓存再执行构建:
    "build": "rm -rf node_modules/.cache && cross-env NODE_OPTIONS=--max-old-space-size=3072 vite build --out-dir build"
    

内容的提问来源于stack exchange,提问作者Archan Bhalani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 18:23:13