Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here's the easiest way to read lines from a file, without any external modules:

const fs = require('fs');
const readline = require('readline');

async function processLineByLine() {
  const fileStream = fs.createReadStream('input.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });
  // Note: we use the crlfDelay option to recognize all instances of CR LF
  // ('\r\n') in input.txt as a single line break.

  for await (const line of rl) {
    // Each line in input.txt will be successively available here as `line`.
    console.log(`Line from file: ${line}`);
  }
}

processLineByLine();

Or alternatively:

var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('file.in')
});

lineReader.on('line', function (line) {
  console.log('Line from file:', line);
});

lineReader.on('close', function () {
    console.log('all done, son');
});

The last line is read correctly (as of Node v0.12 or later), even if there is no final \n.

UPDATE: this example has been added to Node's API official documentation.

Answer from Dan Dascalescu on Stack Overflow
๐ŸŒ
Node.js
nodejs.org โ€บ en โ€บ learn โ€บ manipulating-files โ€บ reading-files-with-nodejs
Node.js โ€” Reading files with Node.js
Collecting code coverage in Node.js ยท The simplest way to read a file in Node.js is to use the fs.readFile() method, passing it the file path, encoding and a callback function that will be called with the file data (and the error): CJSESM ยท ...
๐ŸŒ
Futurestud.io
futurestud.io โ€บ tutorials โ€บ node-js-read-file-content-as-string
Node.js โ€” Read File Content as String
July 29, 2021 - This package comes with a handy content(path) method reading and returning the content of the given file path as a string: const Fs = require('@supercharge/filesystem') const text = await Fs.content('./existing-file.txt') // 'Hello Marcus. Dude, you look super sharp today :)' ... Get your weekly push notification about new and trending Future Studio content and recent platform enhancements ... Marcus is a fullstack JS developer. Heโ€™s passionate about the hapi framework for Node.js and loves to build web apps and APIs.
Discussions

Read a file in Node.js - Stack Overflow
I'm quite puzzled with reading files in Node.js. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Best way to read/write/parse complex binary files in node.js
Okay so I might be missing the point here, but nodejs streams do not do any conversions of sorts when handeling read/write, so if the purpose is to keep the structures as in the binary file to send it somewhere else, then thats the way to go. You can also pipe a stream to a process that does conversions if you need it and pipe it to your endpoint. Like middleware. More on reddit.com
๐ŸŒ r/node
8
11
June 30, 2018
S3 getObject for a ZIP file and read contents node JS

Something like this?

const AWS = require('aws-sdk');
const JSZip = require('jszip');

const s3 = new AWS.S3();

// Add your getObject params
const params = {};

s3.getObject(params)
    .promise()
    .then((data) => {
        return JSZip.loadAsync(data.Body);
    })
    .then((zip) => {
        // Do stuff with the zip contents
        // JSZip Docs: https://stuk.github.io/jszip/
    });
More on reddit.com
๐ŸŒ r/aws
5
4
October 30, 2017
How to compress a large string with gzip to a file?
You don't need a readable or a duplex stream if you already have the data live in memory. Pipe the gzip to the output stream and then just write the data to the gzip. gzip.write(data); gzip.end(); If you are using zlib correctly I don't know I haven't read the docs you should be good. More on reddit.com
๐ŸŒ r/node
2
1
December 5, 2018
Top answer
1 of 16
1152

Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here's the easiest way to read lines from a file, without any external modules:

const fs = require('fs');
const readline = require('readline');

async function processLineByLine() {
  const fileStream = fs.createReadStream('input.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });
  // Note: we use the crlfDelay option to recognize all instances of CR LF
  // ('\r\n') in input.txt as a single line break.

  for await (const line of rl) {
    // Each line in input.txt will be successively available here as `line`.
    console.log(`Line from file: ${line}`);
  }
}

processLineByLine();

Or alternatively:

var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('file.in')
});

lineReader.on('line', function (line) {
  console.log('Line from file:', line);
});

lineReader.on('close', function () {
    console.log('all done, son');
});

The last line is read correctly (as of Node v0.12 or later), even if there is no final \n.

UPDATE: this example has been added to Node's API official documentation.

2 of 16
180

For such a simple operation there shouldn't be any dependency on third-party modules. Go easy.

var fs = require('fs'),
    readline = require('readline');

var rd = readline.createInterface({
    input: fs.createReadStream('/path/to/file'),
    output: process.stdout,
    console: false
});

rd.on('line', function(line) {
    console.log(line);
});

rd.on('close', function() {
    console.log('all done, son');
});
๐ŸŒ
W3Schools
w3schools.com โ€บ nodejs โ€บ nodejs_filesystem.asp
Node.js File System Module
Best Practice: Always specify the character encoding (like 'utf8') when reading text files to get a string instead of a Buffer. Node.js provides several methods for creating and writing to files.
๐ŸŒ
Memberstack
memberstack.com โ€บ home โ€บ blog โ€บ product โ€บ the ultimate guide to reading files in node.js
The Ultimate Guide to Reading Files in Node.js | Memberstack
March 23, 2022 - In the example provided above, we used the fs.createWriteStream() function to create a file stream that allows us to write raw data to this file, and then we used Node.js' built-in https package to send a request to a sample image hosted on Imgur. After that, we piped the result from our request to our file, so when you run the code above, the image file from our remote URL is downloaded as a new file โ€” image.jpg โ€” and saved on our local machine. We can read JSON files following the same process we did for reading a .txt and the .html file in our previous examples.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-read-and-write-files-with-nodejs
How to Read and Write Files with Node.js
August 19, 2024 - So you should understand how to read and write files. Files are the backbone of data storage. Node.js provides a powerful 'fs' (file system) module to interact with these files seamlessly. Let's assume I want to read a JSON file in Node.js.
Find elsewhere
๐ŸŒ
Honeybadger
honeybadger.io โ€บ blog โ€บ file-operations-node
File operations in Node.js - Honeybadger Developer Blog
February 27, 2024 - Node.js provides a built-in fs module that allows us to perform various file operations. Whether it's reading from or writing to files, handling different file formats, or managing directories, Node.js offers versatile functionalities to interact with the file system.
๐ŸŒ
Code Maven
code-maven.com โ€บ reading-a-file-with-nodejs
Reading a file with Node.js - blocking and non-blocking
For example to read the content of a file. The "normal" way in Node.js is probably to read in the content of a file in a non-blocking, asynchronous way. That is, to tell Node to read in the file, and then to get a callback when the file-reading has been finished.
๐ŸŒ
Medium
medium.com โ€บ @dalaidunc โ€บ fs-readfile-vs-streams-to-read-text-files-in-node-js-5dd0710c80ea
fs.readFile vs streams to read text files in node.js | by Duncan Grant | Medium
August 22, 2017 - The de facto standard of reading text files in node.js is to use fs.readFile(filename, โ€œutf8"). Itโ€™s quick and simple to write code usingโ€ฆ
๐ŸŒ
Geshan
geshan.com.np โ€บ blog โ€บ 2021 โ€บ 10 โ€บ nodejs-read-file-line-by-line
4 ways to read file line by line in Node.js
October 8, 2021 - Learn how to read file line by line in Node.js with sync and async methods using native and NPM modules.
๐ŸŒ
TutorialEdge
tutorialedge.net โ€บ nodejs โ€บ reading-writing-files-with-nodejs
Reading and Writing Files With NodeJS | TutorialEdge.net
April 15, 2017 - var fs = require("fs"); fs.readFile("temp.txt", function(err, buf) { console.log(buf.toString()); }); Create a temp.txt within the same directory and write in it anything youโ€™d like. Run your script using node app.js and you should see in the console the contents of your file.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ node.js โ€บ node-js-fs-readfile-method
Node JS fs.readFile() Method - GeeksforGeeks
July 12, 2025 - The method does not return a value. Instead, it invokes the provided callback function with the file's contents or an error. โ€‹In Node.js, there are two primary methods for reading files: fs.readFile() and fs.readFileSync().
๐ŸŒ
Inari
devhunt.org โ€บ blog โ€บ nodejs-readfile-for-beginners
Nodejs Readfile for Beginners
September 16, 2025 - Learn how to read and process files in Node.js with this comprehensive guide. Master file reading, line-by-line processing, and converting files to strings with practical examples and explanations.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ fundamentals-of-text-data-manipulation-in-js โ€บ lessons โ€บ reading-files-line-by-line-in-javascript-using-nodejs
Reading Files Line-by-Line in JavaScript Using Node.js
In JavaScript, particularly when using Node.js, we use the fs module to handle file operations. To open a file and read its contents, we'll make use of the fs.readFileSync() function. This allows us to read the entire file into a string.
๐ŸŒ
Nodejsdesignpatterns
nodejsdesignpatterns.com โ€บ blog โ€บ reading-writing-files-nodejs
Reading and Writing Files in Node.js - The Complete Modern Guide
October 12, 2025 - Learn the modern way to read and write files in Node.js using promises, streams, and file handles. Master memory-efficient file operations for production applications.
Authors ย  Mario CasciaroLuciano Mammino
Published ย  2024
Pages ย  660
Rating: 4.6 โ€‹ - โ€‹ 780 votes
๐ŸŒ
Node.js
nodejs.org โ€บ api โ€บ fs.html
File system | Node.js v25.8.1 Documentation
An example of reading a package.json file located in the same directory of the running code: import { readFile } from 'node:fs/promises'; try { const filePath = new URL('./package.json', import.meta.url); const contents = await readFile(filePath, { encoding: 'utf8' }); console.log(contents); ...
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ reading and writing json files in node.js: a complete tutorial
Reading and writing JSON files in Node.js: A complete tutorial - LogRocket Blog
November 1, 2024 - Make sure to deserialize the JSON string passed to the callback function before you start working with the resulting JavaScript object. You can use the require function to synchronously load JSON files in Node.
๐ŸŒ
Node.js
nodejs.org โ€บ api โ€บ stream.html
Stream | Node.js v25.8.1 Documentation
Because a single read() call does not return all the data, using a while loop may be necessary to continuously read chunks until all data is retrieved. When reading a large file, .read() might return null temporarily, indicating that it has consumed all buffered content but there may be more data yet to be buffered.
๐ŸŒ
Node.js
nodejs.org โ€บ en โ€บ learn โ€บ manipulating-files โ€บ nodejs-file-paths
Node.js โ€” Node.js File Paths
You include this module in your files using const path = require('node:path'); and you can start using its methods. Given a path, you can extract information out of it using those methods: ... const path = require('node:path'); const notes = '/users/joe/notes.txt'; path.dirname(notes); // /users/joe path.basename(notes); // notes.txt path.extname(notes); // .txt ... In this case Node.js will simply append /joe.txt to the current working directory.