5 Ways To Read Files In NodeJS (To String, Line-by-Line, Array)

Welcome to a quick tutorial and examples on how to read files in NodeJS. Reading files, just how difficult can it be? Well, brace yourselves. It’s not that straightforward apparently.

The common ways to read files in NodeJS are:

  1. To read the entire file into a string asynchronously – require("fs").readFile("FILE.TXT", "utf8", (err, data) => { console.log(data); });
  2. Read a file into a string synchronously – var data = require("fs").readFileSync("FILE.TXT", "utf8");
  3. Read a file line-by-line.
    • const fs = require("fs"), rl = require("readline");
    • const reader = rl.createInterface({ input: fs.createReadStream("FILE.TXT") });
    • reader.on("line", row => console.log(row));
  4. To read a file into an array – require("fs").readFile("FILE.TXT", "utf8", (err, data) => data = data.split("\r\n"));
  5. Read a file on a remote server –
    • require("https")
    • .get("https://code-boxx.com", res => { res.on("data", d => process.stdout.write(d)); });

That covers the quick basics, but read on for more examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

NODEJS READ FILES

All right, let us now get into the examples of how to read files in NodeJS.

 

METHOD 1) READ FILE INTO STRING (ASYNCHRONOUSLY)

1-readfile-async.js
// (A) FILE SYSTEM MODULE
const fs = require("fs");
 
// (B) READ FILE INTO STRING
fs.readFile("README.txt", "utf8", (err, data) => {
  // (B1) OPTIONAL - HANDLE ERROR
  if (err) { console.log(err); }
 
  / (B2) FILE DATA
  console.log(data);
  console.log(typeof data);
});

This is the most common and simplest way to read a file in NodeJS. Just use the File System module require("fs").readFile() to fetch all the file contents into a string.

For the uninitiated – “Asynchronous” will read the file in parallel. That is, if there is more code below fs.readFile(), it will continue to run while the file reads in parallel.

 

METHOD 2) READ FILE INTO STRING (SYNCHRONOUSLY)

2-readfile-sync.js
// (A) FILE SYSTEM MODULE
const fs = require("fs");
 
// (B) READ FILE INTO STRING (SYNC)
try { var data = fs.readFileSync("README.txt", "utf8"); }
catch (err) { console.log(err); }
 
// (C) FILE DATA
console.log(data);
console.log(typeof data);

This is pretty much the same as the previous example, except that it’s synchronous. That is, fs.readFileSync() must complete reading before the script below can run.

 

 

METHOD 3) READ FILE LINE-BY-LINE

3-line-by-line.js
// (A) FILE SYSTEM + READLINE MODULES
const fs = require("fs"),
      rl = require("readline");
 
// (B) CREATE READ STREAM
const reader = rl.createInterface({
  input: fs.createReadStream("README.txt")
});
 
// (C) READ LINES
var count = 1;
reader.on("line", row => {
  console.log(`Line ${count} : ${row}`);
  count++;
});

This is the so-called “roundabout way” to read a file, create a read stream to extract line-by-line. Why the extra trouble? Because reading a massive file all at once into a single string is a bad idea – It’s bad for performance and will probably cause an “out-of-memory” problem.

 

METHOD 4) READ FILE INTO ARRAY

4a-array.js
// (A) FILE SYSTEM MODULE
const fs = require("fs");

// (B) READ FILE INTO STRING
fs.readFile("README.txt", "utf8", (err, data) => {
  // (B1) SPLIT LINES INTO ARRAY
  data = data.split("\r\n");

  // (B2) FILE DATA
  console.log(data);
  console.log(typeof data);
  console.log(data.length);
});
4b-array.js
// (A) FILE SYSTEM + READLINE MODULES
const fs = require("fs"),
      rl = require("readline");
 
// (B) CREATE READ STREAM
const reader = rl.createInterface({
  input: fs.createReadStream("README.txt")
});
 
// (C) READ LINES
var data = [];
reader.on("line", row => data.push(row));
 
// (D) FINISHED READING ENTIRE FILE
reader.on("close", () => {
  console.log(data);
  console.log(typeof data);
  console.log(data.length);
});

If you want to read a file into an array, there’s no straightforward way to do it. So it’s either:

  • Read the entire file into a string, then split("\r\n").
  • Read the file line-by-line, and collect each row into an array.

 

 

METHOD 5) READ REMOTE FILE

5-remote.js
// (A) HTTPS REQUEST MODULE
const https = require("https");
 
// (B) GET REQUEST
https.get("https://en.wikipedia.org/wiki/Aha_ha", res => {
  // (B1) COLLECT DATA
  let data = "";
  res.on("data", chunk => data += chunk);

  // (B2) ON TRANSFER COMPLETE
  res.on("end", () => console.log(data));
})
 
// (B3) OPTIONAL - HANDLE ERRORS
.on("error", err => console.log(err));

Lastly and yes, NodeJS is capable of making HTTP requests – Fetch and read files on remote servers.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *