node打包报错解决FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap
增加 Node.js 堆内存限制。可以在运行构建命令时,通过 NODE_OPTIONS 环境变量来增大 Node.js 的堆内存限制。
·
在执行 pnpm run build:uat 命令期间,项目构建进程因 JavaScript 堆内存不足而崩溃。表明构建进程在运行时耗尽了分配给 Node.js 的内存,从而导致程序崩溃。
报错信息
<--- JS stacktrace --->
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 0xb9c1f0 node::Abort() [node]
2: 0xaa27ee [node]
3: 0xd73950 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
4: 0xd73cf7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
5: 0xf51075 [node]
6: 0xf51f78 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
7: 0xf62473 [node]
8: 0xf632e8 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
9: 0xf3dc3e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
10: 0xf3f007 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
11: 0xf1f580 v8::internal::Factory::AllocateRaw(int, v8::internal::AllocationType, v8::internal::AllocationAlignment) [node]
12: 0xf16b4c v8::internal::FactoryBase<v8::internal::Factory>::AllocateRawArray(int, v8::internal::AllocationType) [node]
13: 0xf16cc5 v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArrayWithFiller(v8::internal::Handle<v8::internal::Map>, int, v8::internal::Handle<v8::internal::Oddball>, v8::internal::AllocationType) [node]
14: 0x10c7972 [node]
15: 0x10d05e4 [node]
16: 0x10fbc98 [node]
17: 0x115d3dd v8::internal::JSObject::AddDataElement(v8::internal::Handle<v8::internal::JSObject>, unsigned int, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyAttributes) [node]
18: 0x11c5f53 v8::internal::Object::AddDataProperty(v8::internal::LookupIterator*, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyAttributes, v8::Maybe<v8::internal::ShouldThrow>, v8::internal::StoreOrigin, v8::internal::EnforceDefineSemantics) [node]
19: 0x11c6d66 v8::internal::Object::SetProperty(v8::internal::LookupIterator*, v8::internal::Handle<v8::internal::Object>, v8::internal::StoreOrigin, v8::Maybe<v8::internal::ShouldThrow>) [node]
20: 0x12f0a30 v8::internal::Runtime_SetKeyedProperty(int, unsigned long*, v8::internal::Isolate*) [node]
21: 0x17120b9 [node]
ELIFECYCLE Command failed with exit code 1.
解决方式
增加 Node.js 堆内存限制。可以在运行构建命令时,通过 NODE_OPTIONS 环境变量来增大 Node.js 的堆内存限制。cross-env NODE_OPTIONS=--max-old-space-size=4096
修改 package.json 中的脚本:
"scripts": {
"bootstrap": "pnpm install",
"serve": "pnpm run dev",
"dev": "vite",
"build": "cross-env NODE_OPTIONS=--max-old-space-size=4096 cross-env NODE_ENV=production vite build && esno ./build/script/postBuild.ts",
"build:test": "cross-env NODE_OPTIONS=--max-old-space-size=4096 vite build && esno ./build/script/postBuild.ts --mode test",
"build:uat": "cross-env NODE_OPTIONS=--max-old-space-size=4096 vite build && esno ./build/script/postBuild.ts --mode uat",
"build:no-cache": "pnpm clean:cache && pnpm run build",
更多推荐
所有评论(0)