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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Read Files Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

NODEJS READ FILES

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

 

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') to fetch all the file contents into a string.

 

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.

 

 

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.

 

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, collect each row into an array.

 

 

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.

 

 

USEFUL BITS & LINKS

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

 

INFOGRAPHIC CHEAT SHEET

Read Files In NodeJS (Click To Enlarge)

 

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 *