npm run dev超内存了怎么办?
Posted Y飞羽Y
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了npm run dev超内存了怎么办?相关的知识,希望对你有一定的参考价值。
某个很庞大的前端项目,npm run dev时modules达到了7000+个
能够成功运行npm run dev,但稍微改一下代码,dev重载一下,就立马报错退出了,报错内容:
<--- Last few GCs --->
[20848:000001604840F2A0] 315301 ms: Mark-sweep 1246.0 (1351.6) -> 1244.6 (1334.7) MB, 434.2 / 0.1 ms (average mu = 0.912, current mu = 0.000) last resort GC in old space requested
[20848:000001604840F2A0] 315741 ms: Mark-sweep 1244.6 (1334.7) -> 1244.6 (1326.7) MB, 440.1 / 0.1 ms (average mu = 0.833, current mu = 0.000) last resort GC in old space requested
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0000018CB7ADC5C1]
Security context: 0x0124a8f1e6e9 <JSObject>
1: byteLength(aka byteLength) [0000009E0A5F9241] [buffer.js:531] [bytecode=000003C658370D49 offset=204](this=0x03d0ef5826f1 <undefined>,string=0x01361cd924e9 <Very long string[102496108]>,encoding=0x0124a8f3e2e1 <String[4]: utf8>)
2: arguments adaptor frame: 3->2
3: fromString(aka fromString) [000002BF07A93251] [buffer.js:342] [bytecode=0000037...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - javascript heap out of memory
1: 00007FF7F3A1C6AA v8::internal::GCIdleTimeHandler::GCIdleTimeHandler+4506
2: 00007FF7F39F7416 node::MakeCallback+4534
3: 00007FF7F39F7D90 node_module_register+2032
4: 00007FF7F3D1189E v8::internal::FatalProcessOutOfMemory+846
5: 00007FF7F3D117CF v8::internal::FatalProcessOutOfMemory+639
6: 00007FF7F3EF7F94 v8::internal::Heap::MaxHeapGrowingFactor+9620
7: 00007FF7F3EF645B v8::internal::Heap::MaxHeapGrowingFactor+2651
8: 00007FF7F40202BB v8::internal::Factory::AllocateRawWithImmortalMap+59
9: 00007FF7F4022D6D v8::internal::Factory::NewRawTwoByteString+77
10: 00007FF7F3D6DDA8 v8::internal::Smi::SmiPrint+536
11: 00007FF7F3D04EAB v8::internal::StringHasher::UpdateIndex+219
12: 00007FF7F3D2A2C6 v8::String::Utf8Length+22
13: 00007FF7F39E07AC node::Buffer::New+4332
14: 00007FF7F3F24382 std::vector<v8::internal::compiler::MoveOperands * __ptr64,v8::internal::ZoneAllocator<v8::internal::compiler::MoveOperands * __ptr64> >::_Umove+79442
15: 00007FF7F3F2580D std::vector<v8::internal::compiler::MoveOperands * __ptr64,v8::internal::ZoneAllocator<v8::internal::compiler::MoveOperands * __ptr64> >::_Umove+84701
16: 00007FF7F3F24866 std::vector<v8::internal::compiler::MoveOperands * __ptr64,v8::internal::ZoneAllocator<v8::internal::compiler::MoveOperands * __ptr64> >::_Umove+80694
17: 00007FF7F3F2474B std::vector<v8::internal::compiler::MoveOperands * __ptr64,v8::internal::ZoneAllocator<v8::internal::compiler::MoveOperands * __ptr64> >::_Umove+80411
18: 0000018CB7ADC5C1
其中关键内容:JavaScript heap out of memory
这是因为nodejs默认64位系统运行内存为1.7GB,前端项目太过庞大,dev所需的内存已超过这个限制所致。
如何修改nodejs的运行内存:
# 先安装两个插件
npm install --save-dev increase-memory-limit
npm install --save-dev cross-env
#修改package.json
"scripts":
"dev": "vue-cli-service serve",
"fix-memory-limit": "cross-env LIMIT=4096 increase-memory-limit" //添加这一行
,
# 然后运行命令
npm run fix-memory-limit
# 然后网络上说就可以正常启动了
npm run dev
但是我在此时,运行npm run dev出现了
‘“node --max-old-space-size=4096”’ 不是内部或外部命令,也不是可运行的程序
这是因为fix-memory-limit替换命令时多加了引号所致。即%_prog%被替换成了“%_prog%”导致出错。
解决方案:
使用全局替换,将node_modules/.bin/*.cmd中的所有“%_prog%”改为%_prog%,完成.
此时再npm run dev 就能够正常运行了
以上是关于npm run dev超内存了怎么办?的主要内容,如果未能解决你的问题,请参考以下文章
npm run dev 报错:missing script:dev
npm run prod 和 dev 中的 TailwindUI 差异
npm init vite@latest; 项目名字是abcde,选了vue-ts; cd abcde; npm install; npm run dev;浏览器访问,结果是空白的,这是怎么回事?
在打包(npm run build)的时候不执行了,但是也不报错,输入npm run dev可以启动
关于vue的npm run dev和npm run build
npm run dev 报错 run `npm audit fix` to fix them, or `npm audit` for details