This article introduces to you the solutions to problems encountered when reading large text files in nodejs. Friends in need can refer to it.
Been playing around with NodeJS lately and encountered the following error when trying to read a very large text file:
FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory
The following solution allows you to stream the file instead of reading it in its entirety Into memory:
var fs = require('fs'); var readline = require('readline'); var stream = require('stream'); var instream = fs.createReadStream('your/file'); var outstream = new stream; var rl = readline.createInterface(instream, outstream); rl.on('line', function(line) { // process line here }); rl.on('close', function() { // do something on finish here });
The above is the detailed content of Solutions to problems encountered when reading large text files in nodejs. For more information, please follow other related articles on the PHP Chinese website!