javascript JSON.stringify 抛出 RangeError: Invalid string length for large objects

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29175877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:10:05  来源:igfitidea点击:

JSON.stringify throws RangeError: Invalid string length for huge objects

javascriptjsonnode.jsstringify

提问by borisdiakur

As the title implies I'm trying to stringify huge JavaScript Object with JSON.stringifyin my Node.js app. The objects are - again - huge (tens of mega bytes), they don't contain any functions. I need to write the serialized objects to a file. What I'm getting now is this:

正如标题所暗示的那样,我试图JSON.stringify在我的 Node.js 应用程序中对巨大的 JavaScript 对象进行字符串化。这些对象 - 再次 - 巨大(数十兆字节),它们不包含任何功能。我需要将序列化对象写入文件。我现在得到的是:

RangeError: Invalid string length
  at Object.stringify (native)
  at stringifyResult (/my/file.js:123:45) -> line where I use JSON.stringify

Any idea how to solve that issue?

知道如何解决这个问题吗?

回答by Scott Stensland

I too have seen this unhelpful/misleading nodejs error message, so I booked an issue over at nodejs github

我也看到了这个无用/误导性的 nodejs 错误消息,所以我在 nodejs github 上预订了一个问题

RangeError: Invalid string length --- it should be saying Out Of Memory

RangeError: Invalid string length --- 它应该是 Out Of Memory