Shubham Chadokar
AuthorI am a Software Engineer who loves to write articles and tutorials on the latest technologies. I write majorly on Nodejs and Golang.
For enquiries call:
+1-469-442-0620
HomeBlogWeb DevelopmentHow to read files with Node.js?
In this tutorial, we will explore different ways we can read files in Node.js.
In software development, reading files is a regular part of the job. However, this simple process very often becomes complicated and tedious. Fortunately, that's not the case in Node.js. Node.js provides a built-infile system(fs) module, to carry out file operations.
Before we start, let us make sure we have the basic prerequisites. You will need to ensure you have:
For this tutorial, my environment setup includes:
fs module is a built-in module in the Node.js. You do notnecessarily need to install it.
The fs module supports both Synchronous and Asynchronous function calls.
I am assuming you have installed the NodeJS.
Lorem ipsum dolor sit amet, consecteturadipiscingelit, sed do eiusmodtemporincididuntutlabore et dolore magna aliqua.
Open this directory in the code editor.
From the index.js, we will read the test.txt.
The readFileSync is a synchronous method to read a file.
Syntax
fs.readFileSync(path[, options])
Open index.js in the Code Editor.
Paste the below code.
// import the fs module const fs = require("fs"); // read file synchronously const readData = fs.readFileSync("./data.txt"); console.log(readData);
Open the terminal or cmd in the node-fs directory and run the below command.
$ node index.js <Buffer 4c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74 2c 20 63 6f 6e 73 65 63 74 65 74 75 72 20 61 64 69 70 69 73 63 69 6e 67 ... 73 more bytes>
It has returned the content in the buffer format. The readFileSync by default returns the content in the Buffer format.
To get the content in normal string format, we need to pass the utf8 encoding as a parameter.
Change the code.
// import the fs module const fs = require("fs"); // read file synchronously const readData = fs.readFileSync("./data.txt", "utf8"); console.log(readData);
run it.
$ node index.js
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Read file Asynchronously using readFile
Asynchronous methods are great when you don’t want to wait for a process to complete.
fs module provides an asynchronous method to read a file. It returns a callback.
Syntax
fs.readFileSync(path[, options], callback)
Syntax is similar tothe synchronous method except it returns a callback function.
// import the fs module const fs = require("fs"); // read file asynchronously fs.readFile("./data.txt", "utf8", (err, data) => { if (!err) { console.log(data); } else { console.error("Something went wrong while reading the file."); } });
Run the code.
$ node index.js
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Reading file using fs.readFileis an ideal way if the file size is less. If the size is huge, the read will consume the complete Memory and you may find your system hanging.
In such scenarios, Stream is the best option. This breaks the file into small chunks of data and sends a piece at a time. In this way, the system can continue with other tasks without allocating the complete memory to it.
Let us code and understand.
Node.js provides a createReadStreammethod to stream the file. createReadStreamreturns a fs.ReadStreamobject. The readStream events like data, end, or error. To read the file, we have to listen to these events.
// import the fs module constfs = require("fs"); // read file using stream const streamRead = async () => { return new Promise((resolve, reject) => { try { // create a read stream const streamRead = fs.createReadStream("./data.txt", { encoding:"utf8", }); // listen to data event streamRead.on("data", (data) => { console.log(data); }); // listen to end event streamRead.on("end", () => { resolve("File read complete"); }); // listen to error event streamRead.on("error", (error) => { throw Error( "Something went wrong while streaming the file.", error.message ); }); } catch (error) { reject(error); } }); }; streamRead().then((res, err) => { if(!err) console.log(res); else console.error(err); });
Run the code.
$ node index.js
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. File read complete
For the purpose of the experiment, I created a file of 20MB to check the performance of readFileSync, readFileand createReadStream.
The average memory consumption of VS Code is 600-700MB in my machine.
This is a screenshot before running any of the commands.
Looking to enhance your coding skills? Discover the best online Python course with certificate. Unlock endless possibilities and become a Python pro today!
Check the screenshot. This operation took 373MB and it went on to upto ~450MB.
Check the screenshot. This operation took 506MB and it went on to upto ~600MB.
Check the screenshot. This operation took 287MB and it reached upto ~300MB.
Conclusion
Node.js provides multiple methods to read a file. It completely depends on the file size and the requirement, which method to choose. From our performance tests, streaming turns out to be the most efficient method to use.
The performance may vary from machine to machine and file size. One must note that what we have demonstrated is just for the sake of a demo, so this must not be taken to be the final words. There are other npm packages available for file system, for example,fs-extra.
Hope this tutorial has helped. Taking a formal training course in Node.js would be a great way to fast-track your career.
Happy coding!
Tired of fixing bugs? Find easy solutions to your programming queries with our live, interactive workshops. Explore now.
Name | Date | Fee | Know more |
---|