Nuxt3项目GitHub Action部署遇JavaScript堆内存溢出问题求助
问题
我有一个通过GitHub Action自动部署到Deno Deploy的Nuxt 3应用。今日向main分支提交代码后,部署工作流出现失败,错误日志如下:
[success] Server built in 7521ms [success] [nitro] Generated public .output/public [info] [nitro] Initializing prerenderer [info] [nitro] Prerendering 1 routes [log] [nitro] ├─ /api/_content/cache.1690626485326.json (1414ms) [info] [nitro] Building Nitro Server (preset: `deno-deploy`) <--- Last few GCs ---> [2052:0x6a65f60] 77941 ms: Mark-sweep 2000.7 (2080.7) -> 1996.3 (2093.3) MB, 1926.7 / 0.0 ms (average mu = 0.525, current mu = 0.107) allocation failure; scavenge might not succeed [2052:0x6a65f60] 80549 ms: Mark-sweep 2012.1 (2093.3) -> 2006.6 (2102.6) MB, 2569.1 / 0.0 ms (average mu = 0.297, current mu = 0.015) allocation failure; scavenge might not succeed <--- JS stacktrace ---> FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 1: 0xb83f50 node::Abort() [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 2: 0xa94834 [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 3: 0xd647c0 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 4: 0xd64b67 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 5: 0xf42265 [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 6: 0xf5474d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 7: 0xf2ee4e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 8: 0xf30217 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 9: 0xf113ea v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 10: 0x12d674f v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] 11: 0x17035b9 [/opt/hostedtoolcache/node/18.17.0/x64/bin/node] Aborted (core dumped) error Command failed with exit code 134. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. Error: Process completed with exit code 134.
该应用在本地机器上构建无问题,此次提交仅配置了Sentry和Nuxt Image,未添加可能导致内存超限的内容,且已回滚至上次成功构建的版本,但问题仍未解决。
解决方案
- 增加Node.js堆内存限制:GitHub Action默认runner内存有限,在构建环节添加内存参数。修改package.json的build命令为:
"build": "NODE_OPTIONS=--max-old-space-size=4096 nuxt build";或者在GitHub Action工作流的build步骤中设置环境变量:- name: Build env: NODE_OPTIONS: "--max-old-space-size=4096" run: yarn build - 优化Runner资源配置:默认ubuntu-latest runner内存约7GB,若并发任务占用资源,可尝试指定更大内存的runner,或使用自托管runner(有条件的话)。
- 精简Nuxt构建逻辑:
- 禁用非必要预渲染路由,仅保留关键页面预渲染;
- 调整Nuxt Image配置,改用CDN处理图片,减少构建时本地图片处理的内存消耗;
- 在GitHub Action中添加缓存清理步骤:
- name: Clean dependencies and cache run: | yarn cache clean rm -rf node_modules yarn install --frozen-lockfile
- 切换Node.js版本:当前使用Node.js 18.17.0,尝试切换到18.16.0或16.x稳定版本,部分版本内存管理更稳定。修改工作流中的Node安装步骤:
- name: Use Node.js 18.16.0 uses: actions/setup-node@v3 with: node-version: '18.16.0' cache: 'yarn' - 更新依赖修复已知问题:执行
yarn upgrade nuxt nitro更新Nuxt和Nitro到最新版本,修复可能存在的内存泄漏问题。
内容的提问来源于stack exchange,提问作者Colibri
相关产品推荐
相关产品推荐

