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 
    1: set(this=0x3aa6e5392fe1 ,0x3aa6ae84a549 ,0x3aa6ae84a651 )
    2: /* anonymous */ [/Users/Hanger/GitHub/myjs/node_modules/[email protected]@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::Rehash(v8::internal::Handle, 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,问题解决

你可能感兴趣的:(vue3,npm,javascript,前端)