Home  >  Article  >  Web Front-end  >  Solutions to problems encountered when reading large text files in nodejs

Solutions to problems encountered when reading large text files in nodejs

不言
不言Original
2018-11-08 15:11:463600browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn