Node.js › File System (fs) Module
How would you read a large file efficiently using a stream rather than loading it entirely into memory with fs.readFile()?
IntermediateAnswer
fs.createReadStream() reads a file incrementally in chunks, emitting 'data' events (or usable via .pipe() or async iteration) as each chunk becomes available, rather than buffering the entire file's contents in memory at once -- essential for processing files larger than available memory, or simply for reducing memory pressure and improving responsiveness when handling large files like log files or video uploads.
const fs = require('node:fs');
const readStream = fs.createReadStream('huge-log-file.txt', { encoding: 'utf-8', highWaterMark: 64 * 1024 });
for await (const chunk of readStream) {
processChunk(chunk); // processes incrementally, without loading the whole file into memory
}Real-world example A log-analysis tool that previously used fs.readFile() and crashed with an out-of-memory error on multi-gigabyte log files is rewritten to use fs.createReadStream(), processing the file incrementally in manageable chunks and successfully handling files of any size regardless of available memory.