Nuxt 生成致命错误:在堆限制附近的无效标记压缩分配失败 - JavaScript 堆内存不足

Posted

技术标签:

【中文标题】Nuxt 生成致命错误:在堆限制附近的无效标记压缩分配失败 - JavaScript 堆内存不足【英文标题】:Nuxt generate FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 【发布时间】:2019-02-11 19:14:18 【问题描述】:

我正在尝试nuxt generate 我的静态(带有动态 url 参数)html 页面。 这是我在nuxt.config.js 文件中的路由配置

routes: function () 
      let domain = 'https://example.com'
      if (process.env.NUXT_ENV === 'dev' || process.env.NUXT_ENV === 'development') 
        domain = 'https://dev-example.com'
      
      if (process.env.NUXT_ENV === 'local') 
        domain = 'http://localhost:3002'
      
      let rooms = axios.get(domain + '/nuxt/rooms').then((res) => 
        if(res && res.data.length)
          return res.data.map((room) => 
            return '/manage/pro/room/' + room._id
          )
        else
          return []
        
      ).catch(response => 
        console.log('errore room')
      );
      let bookings = axios.get(domain + '/nuxt/users').then((res) => 
        if(res && res.data.length)
          return res.data.map((user) => 
            return '/bookings/' + user.slug
          )
        else 
          return []
        
      ).catch(response => 
        console.log('errore rehearsal')
      );
      let user = axios.get(domain + '/nuxt/users').then((res) => 
        if(res && res.data.length)
          return res.data.map((user) => 
            return '/user/' + user._id + '/' + user.username
          )
        else 
          return []
        
      ).catch(response => 
        console.log('errore user public')
      );
      let posts = axios.get(domain + '/nuxt/posts').then((res) => 
        if(res && res.data.length)
          return res.data.map((post) => 
            return '/post/' + post._id
          )
        else 
          return []
        
      ).catch(response => 
        console.log('errore posts')
      );
      return Promise.all([rooms, posts, user, bookings]).then(values => 
        return values.join().split(',');
      )
    
  ,

它一直都很好,并且这个过程一直很成功,直到今天。这似乎需要太多的 RAM 内存。不知道为什么,也不知道如何解决这个问题。 这是最后的控制台输出行

2018-09-06T13:12:34.423Z nuxt:render Rendering url /manage/pro/room/5b3f26783e62155502337f8f

<--- Last few GCs --->

[14687:0x2c19ac0]   657918 ms: Mark-sweep 1339.9 (1440.7) -> 1328.1 (1440.2) MB, 2760.7 / 0.1 ms  (average mu = 0.224, current mu = 0.161) allocation failure scavenge might not succeed
[14687:0x2c19ac0]   661179 ms: Mark-sweep 1341.0 (1440.2) -> 1330.6 (1444.2) MB, 2752.4 / 0.1 ms  (average mu = 0.191, current mu = 0.156) allocation failure scavenge might not succeed


<--- JS stacktrace --->

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

    0: ExitFrame [pc: 0x5b702a041bd]
    1: StubFrame [pc: 0x5b702a14fb2]
Security context: 0x3e9218f9e589 <JSObject>
    2: rules [0x2590b4e7c449] [/node_modules/clean-css/lib/writer/helpers.js:~46] [pc=0x5b704716ec9](this=0x3e348f706519 <JSGlobal Object>,context=0x1a44edae1691 <Object map = 0x169e1dc614d1>,tokens=0x1806c1ded131 <JSArray[2]>)
    3: all [0x2590b4e7c749] [/node_modules/clean-css/lib...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - javascript heap out of memory
 1: node::Abort() [node]
 2: 0x89375c [node]
 3: v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xe616b2 [node]
 6: v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
 7: v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 8: v8::internal::Heap::AllocateRawWithRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [node]
 9: v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [node]
10: v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [node]
11: 0x5b702a041bd

【问题讨论】:

【参考方案1】:

编辑您的package.json 并修改您的启动脚本以使用带有--max-old-space-size=4096 标志的节点 例如,您的 package.json 可能有以下内容:

"scripts": 
    "dev": "nuxt",
    "build": "nuxt build",
    "start": "nuxt start"

改用这个:

"scripts": 
    "dev": "node --max-old-space-size=4096 node_modules/nuxt/bin/nuxt.js nuxt",
    "build": "node --max-old-space-size=4096 node_modules/nuxt/bin/nuxt.js build",
    "start": "node --max-old-space-size=4096 node_modules/nuxt/bin/nuxt.js start"

一些参考:https://medium.com/@vuongtran/how-to-solve-process-out-of-memory-in-node-js-5f0de8f8464c

【讨论】:

"dev": "node --max-old-space-size=4096 node_modules/nuxt/bin/nuxt.js nuxt" 不起作用。 "dev": "node --max-old-space-size=4096 node_modules/nuxt/bin/nuxt.js" 确实:) 对于任何挣扎的人 上面的答案(几乎)适用于我的 2k+ 产品。脚本还需要: "generate": "node --max-old-space-size=4096 node_modules/nuxt/bin/nuxt.js generate"

以上是关于Nuxt 生成致命错误:在堆限制附近的无效标记压缩分配失败 - JavaScript 堆内存不足的主要内容,如果未能解决你的问题,请参考以下文章

如何修复“致命错误:堆限制附近的无效标记压缩分配失败 - JavaScript 堆内存不足”错误

致命错误:接近堆限制的无效标记压缩分配失败 - Ionic 3 中的 JavaScript 堆内存不足

Angular 7 致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足

致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足 Angular 13 应用程序

致命错误:接近堆限制的无效标记压缩分配失败 - 使用 fs 处理大文件时 JavaScript 堆内存不足

错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足