Next.jsでビルド時に「FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed – JavaScript heap out of memory」でビルド失敗
$ yarn build
Creating an optimized production build...
<--- Last few GCs --->
[31498:0x2ab86d0] 229792 ms: Mark-sweep 1277.3 (1459.7) -> 1277.2 (1461.7) MB, 1801.1 / 0.0 ms allocation failure GC in old space requested
[31498:0x2ab86d0] 231676 ms: Mark-sweep 1277.2 (1461.7) -> 1277.0 (1420.7) MB, 1884.4 / 0.0 ms last resort GC in old space requested
[31498:0x2ab86d0] 233210 ms: Mark-sweep 1277.0 (1420.7) -> 1277.0 (1419.2) MB, 1533.2 / 0.0 ms last resort GC in old space requested
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 0xb1707c25879 <JSObject>
0: builtin exit frame: stringify(this=0xb1707c090a9 <Object map = 0x123a95202ba1>,0x11b5562822d1 <undefined>,0x11b5562822d1 <undefined>,0x26b024683529 <Object map = 0x2449e8a328d1>)
1: arguments adaptor frame: 1->3
2: toString [0x11b5562822d1 <undefined>:9522] [bytecode=0xf76e19f6a69 offset=28](this=0xb3c89b82b29 <Object map = 0x306621cb3d19>)
7: /* anonymous */(aka /* anonymous ...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
1: node::Abort() [node]
2: 0x8c21ec [node]
物理メモリが足りている場合は、package.jsonにビルド時のメモリ使用上限を設定します。
package.json
{
"name": "***",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "NODE_OPTIONS=--max_old_space_size=4096 next build",
"start": "next start",
"lint": "next lint"
},
buildオプションに下記を追加します。
"NODE_OPTIONS=--max_old_space_size=4096 next build"