Fixing Node error: “Invalid string length”

If you concatenate strings in Node, you may get an error like so:

$ node select.js
      all_transcripts = all_transcripts + "\n" + transcript;

RangeError: Invalid string length
    at fs.stat (C:\projects\image-annotation\data\talks\json\select.js:24:48)
    at FSReqWrap.oncomplete (fs.js:123:15)

This is actually an out of memory error. The fix is to rewrite your code, so that it operates on each bit of text in sequence, and discard the output. For instance, you might switch to appending the output to a file:

fs.appendFileSync('transcripts.txt', transcript);

Interested in JavaScript? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *