npm run dev/build内存溢出解决方法,亲测有效(JavaScript heap out of memory)

周末在家中从gitlab克隆了公司项目后,正常npm install 后,执行npm run dev时候突然报错了,报错如下

<--- Last few GCs --->

[7030:0x102802400]    90535 ms: Mark-sweep 1412.5 (1534.9) -> 1412.1 (1538.9) MB, 1093.9 / 2.2 ms  allocation failure GC in old space requested
[7030:0x102802400]    91593 ms: Mark-sweep 1412.1 (1538.9) -> 1412.0 (1507.9) MB, 1057.5 / 2.3 ms  last resort GC in old space requested
[7030:0x102802400]    92632 ms: Mark-sweep 1412.0 (1507.9) -> 1412.0 (1507.9) MB, 1039.4 / 2.1 ms  last resort GC in old space requested


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x3aa64faa54d9 <JSObject>
    1: set(this=0x3aa6e5392fe1 <Map map = 0x3aa624f848d9>,0x3aa6ae84a549 <String[194]: /Users/Hanger/GitHub/myjs/node_modules/babel-plugin-transform-es2015-classes/node_modules/babel-helper-function-name/node_modules/babel-template/node_modules/babel-runtime/helpers/createClass.js>,0x3aa6ae84a651 <JSArray[1]>)
    2: /* anonymous */ [/Users/Hanger/GitHub/myjs/node_modules/_webpack@2.7.0@webpack/...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/usr/local/bin/node]
 2: node::OnFatalError(char const*, char const*) [/usr/local/bin/node]
 3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node]
 4: v8::internal::Factory::NewFixedArray(int, v8::internal::PretenureFlag) [/usr/local/bin/node]
 5: v8::internal::OrderedHashTable<v8::internal::OrderedHashMap, 2>::Rehash(v8::internal::Handle<v8::internal::OrderedHashMap>, int) [/usr/local/bin/node]
 6: v8::internal::Runtime_MapGrow(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
 7: 0x2ecd2d1042fd
 8: 0x2ecd2d1b251e

一脸懵逼,仔细看了下发现有一行英文稍微认识点就是句:JavaScript heap out of memory,大概知道些意思:JavaScript内存不足,应为我的项目是vue项目 运行在node环境,所以应该是node内存溢出,于是网上百度了一大堆资料大致如下

  1. 添加 --max-old-space-size
  2. 安装 increase-memory-limit 和cross-env,然后在package.json中的scripts中添加一条命令:如下
"fix-memory-limit": "cross-env LIMIT=8048 increase-memory-limit",

然后 npm run fix-memory-limit 执行完之后 再npm run dev

方案1

首先采用了第一种方法:在启动命令中添加–max-old-space-size,代码如下

"scripts": {
    "dev1": "node --max-old-space-size=4096 ./node_modules/.bin/webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "dev": "cross-env NODE_ENV=development node --max-old-space-size=8048 ./node_modules/.bin/webpack serve --progress --client-log-level silent --config build/webpack.dev.conf.js --host 0.0.0.0",
  },

之后在执行npm run dev,但是还是报错了,同样的错误提示(部分小项目的话可能按此方法操作完后问题就解决了,但是我的还是没解决,可能项目比较大),于是执行第二种方案

方案2
首先下载需要的插件 npm install -g increase-memory-limit cross-env,然后在package.json中的scripts中添加一条命令 “fix-memory-limit”: “cross-env LIMIT=8048 increase-memory-limit”,

"scripts": {
    "fix-memory-limit": "cross-env LIMIT=8048 increase-memory-limit",
    "dev1": "node ./node_modules/.bin/webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "dev": "cross-env NODE_ENV=development node ./node_modules/.bin/webpack serve --progress --client-log-level silent --config build/webpack.dev.conf.js --host 0.0.0.0",
  },

然后执行 npm run fix-memory-limit,再执行 npm run dev,问题解决

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值