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:
- To read the entire file into a string asynchronously –
require("fs").readFile("FILE.TXT", "utf8", (err, data) => { console.log(data); });
- Read a file into a string synchronously –
var data = require("fs").readFileSync("FILE.TXT", "utf8");
- 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));
- To read a file into an array –
require("fs").readFile("FILE.TXT", "utf8", (err, data) => data = data.split("\r\n"));
- 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
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)
// (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)
// (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
// (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
// (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);
});
// (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
// (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
- File System – NodeJS
- Read Line – NodeJS
- HTTPS – NodeJS
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!