欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

webpack打包vue项目CALL_AND_RETRY_LAST Allocation failed

程序员文章站 2024-02-11 11:37:16
...

打包 一个vue.js+electron+webpack项目时,执行yarn run build之后发现卡住了,然后出现如下异常:

D:\Documents\mimic>yarn run build
yarn run v1.9.4
warning package.json: No license field
$ node .electron-vue/build.js && electron-builder

 ___              __                        __                       ___       __
/\_ \       __   /\ \__     ____           /\ \       __  __   __   /\_ \     /\ \
\//\ \    / ,.`\ \ \ ,_\   / ,__\  _______ \ \ \____ /\ \/\ \ /\_\  \//\ \    \_\ \
  \_\ \_ /\  __/  \ \ \/  /\__, `\/\______\ \ \  ,. \\ \ \_\ \\/\ \   \_\ \_ /\ ,. \
  /\____\\ \____\  \ \ \_ \/\____/\/______/  \ \____/ \ \____/ \ \ \  /\____\\ \____\
  \/____/ \/____/   \ \__\ \/___/             \/___/   \/___/   \/_/  \/____/ \/___ /
                     \/__/

  √ building main process
  - building renderer process

<--- Last few GCs --->

[53320:000001EABA0D0AD0]   402245 ms: Mark-sweep 1392.1 (1594.8) -> 1392.1 (1595.3) MB, 5883.0 / 0.0 ms  allocation failure GC in old space requested
[53320:000001EABA0D0AD0]   408532 ms: Mark-sweep 1392.1 (1595.3) -> 1391.9 (1539.8) MB, 6285.2 / 0.0 ms  last resort
[53320:000001EABA0D0AD0]   413294 ms: Mark-sweep 1391.9 (1539.8) -> 1391.9 (1522.8) MB, 4761.0 / 0.0 ms  last resort


<--- JS stacktrace --->

==== JS stack trace =========================================
Security context: 00000019C7029891 <JS Object>
    2: _append [D:\Documents\mimic\node_modules\babel-generator\lib\printer.js:~200] [pc=0000001875BF18B2](this=0000021C1BC88F91 <a Generator with map 000001D966F0C181>,str=000003F551CF9491 <String[1]: =>)
    3: token [D:\Documents\mimic\node_modules\babel-generator\lib\printer.js:~142] [pc=0000001875BE90E1](this=0000021C1BC88F91 <a Generator with map 000001D966F0C181>,str=000003F551CF9491 <...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

搜索到2个方法:

方法1(对我无效):

打开node_modules/.bin/webpack.cmd文件,在node命令后面加上–max-old-space-size=4096。我试了4096、81922 个参数(硬件内存是足够的),都不行。还是会出现上述报错。

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\..\webpack\bin\webpack.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node  "--max-old-space-size=8192 %~dp0\..\webpack\bin\webpack.js" %*
)
方法2(对我有效):

在项目的webpage.js文件中直接进行修改,在build的命令中的node后面加上个空格和–max-old-space-size=4096就可以了。参数可以自行设置,大小的单位为m。注意这里的横杠-必须为英文格式,小心弄错为中文格式导致报错。
如原代码为:

"scripts": {
    "build": "node .electron-vue/build.js && electron-builder"
  },

修改后为:

"scripts": {
    "build": "node -max-old-space-size=4096 .electron-vue/build.js && electron-builder"
  },

该方法可以解决该报错问题。