前端|解决 JavaScript heap out of memory 报错

当我们在 丐版 云服务器中执行 node 命令的时候,如果服务器的内存比较小,介于 JavaScript 的语言特性,只能使用部分内存来进行操作,命令执行可能会报错:

transforming (648) node_modules/axios/lib/axios.js <--- Last few GCs --->[158365:0x4c20f70]27361 ms: Scavenge 473.4 (476.6) -> 472.8 (476.6) MB, 66.4 / 0.0 ms(average mu = 0.266, current mu = 0.331) allocation failure [158365:0x4c20f70]27409 ms: Scavenge 473.7 (476.6) -> 473.5 (477.1) MB, 34.8 / 0.0 ms(average mu = 0.266, current mu = 0.331) allocation failure<--- JS stacktrace --->==== JS stack trace =========================================0: ExitFrame [pc: 0x140de99] Security context: 0x03a356a808d1 1: keys [0x3a356a815b1](this=0x03a356a80969 ,0x04e487f04509 ) 2: parseNode [0x22b7a6be0a31] [/data/ruxf/lolo-admin-console/node_modules/rollup/dist/shared/rollup.js:~2802] [pc=0x109b44f3805c](this=0x04e487f0b621 ,0x04e487f04509 ) ...FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 1: 0xa1a640 node::Abort() [node] 2: 0xa1aa4c node::OnFatalError(char const*, char const*) [node] 3: 0xb9a68e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node] 4: 0xb9aa09 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node] 5: 0xd57c85[node] 6: 0xd58316 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node] 7: 0xd64bd5 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node] 8: 0xd65a85 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node] 9: 0xd6853c v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node] 10: 0xd2ef5b v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node] 11: 0x107158e v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node] 12: 0x140de99[node] [1]158354 abort (core dumped)npm run build

这个时候,从报错信息中可以看到是由于 GC 的内存区太小导致执行出错,所以我们需要增大该内存区域:
export NODE_OPTIONS="--max-old-space-size=8192"

【前端|解决 JavaScript heap out of memory 报错】再次执行即可

    推荐阅读