问题描述
在开发angular项目时,编译过程中出现了内存溢出报错的情况:
Generating browser application bundles (phase: emitting)...Last few GCs
3308:04A3C688]30708 ms:Mark-sweep (reduce)743.4(798.7)->743.3(782.4)MB, 350.3 / 0.0 ms(average mu = 0.221,current mu = 0.001)last resort GC in old space requested3308:04A3C688]31026 ms: Mark-sweep (reduce)743.3(782.4)->743.3(772.7)MB, 318.0 / 0.0 ms(average mu = 0.125.current mu = 0.000)last resort GC in old space requested
- JS stacktrace
ATAL ERROR: CALL AND RETRY LAST Allocation failed-JavaScript heap out of memory
解决方案:
解决办法1
修改
package.json
中的脚本:可以在
package.json
文件中的scripts
字段内修改或添加命令,以在运行特定任务时使用--max-old-space-size
参数。例如"scripts": { "start": "node --max-old-space-size=8192 ./node_modules/@angular/cli/bin/ng serve", "build": "node --max-old-space-size=8192 ./node_modules/@angular/cli/bin/ng build", }
解决办法2
使用
NODE_OPTIONS
环境变量:设置
NODE_OPTIONS
环境变量可以为所有Node.js执行提供默认选项,这意味着Angular CLI和其他Node.js脚本也会受到这个参数的影响。
Unix/Linux/macOS:
export NODE_OPTIONS="--max-old-space-size=8192"
Windows:
set NODE_OPTIONS="--max-old-space-size=8192"
这个设置应该在你的开发环境的初始化脚本中进行,比如
.bashrc
、.zshrc
或Windows的User Variables
中,这样每次新打开终端窗口时都会自动设置。
解决办法3
1:找到目录: node_modules/.bin 里面的 ng.cmd 文件
2:找到目录: node_modules/.bin 里面的 ngc.cmd 文件
分别在这两个文件中 加上此命令“ –max_old_space_size=8192 ”,size大小自己设置。
如上设置若不能生效,尝试刷新项目目录,或者将项目中的 .angular 目录删掉重新编译。
vue应该一样把。暂没测试