Fixing Node error: “Invalid string length”

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

$ node select.js
C:\projects\image-annotation\data\talks\json\select.js:24
      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);

If you want a good Javascript book, I got a lot out of Secrets of the Javascript Ninja.

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 *