Read a file into memory with JavaScript – Complete Guide
Node.js is a JavaScript runtime built on Google’s V8 Engine. On May 27, 2009, Ryan Dahl made it accessible as open-source software. Since then, Node.js has risen in popularity and is now the preferred JavaScript runtime for executing JavaScript on the server side.
Node.js is primarily used to develop the vast majority of server-side Web applications. There is support for almost all of the major databases by libraries and packages. However, there may be times when you need to interface with the file system and read some files to make changes.
This article will discuss various ways to read a file into memory using Node.js. In addition, we will also discuss the different synchronous and asynchronous methods to read or write a file using Node.js. As a bonus, we will look at valuable methods provided to us by Node.js that can aid us in various ways.
The FS Module
The FS Module is a core module included with Node.js. It has been around from the early days of Node.js, dating back to the v0.x releases. The module’s name is an abbreviation for “file system,” It contains all of the functions required to read, write, and delete files on the local machine. Aside from these, the FS module offers many other features for dealing with the file system.
The FS module now adheres to POSIX file system specifications. It implies that the code we develop is somewhat portable between operating systems. Despite the fact that Windows is not a POSIX-compliant operating system, the majority of the FS module’s features will still function.
The FS Module offers two APIs that support synchronous and asynchronous programming styles. Let’s take a look at each one one by one.
Synchronous APIs
The FS Module includes a variety of synchronous APIs for interacting with the file system. These APIs are synchronous. That means file interactions, such as reading, writing, deleting, copying, and so on, are thread-blocking in nature. During these actions, the main thread is halted, and execution restarts after the file operations are done. These API methods are usually the easiest to utilize when you’re just starting.
Let’s discuss some of the most useful synchronous APIs the FS Module provides.
fs.readFileSync()
To read a file synchronously, use the FS Module’s fs.readFileSync()
method. It takes the file path as an argument. Optionally, we can also provide some options as a second argument. These options include the encoding and flag, and the encoding contains data requirements. Its default value is null, which returns the raw buffer, and the flag carries an indicator of file operations. Its defaults to r
. We will convert the buffer to a string to get the file’s content.
To use the FS Module, we first import it using the require()
method. Using ESM, you can import the module using the import
statements.
// CommonJS
const fs = reqruire('fs');
// ESM
import * as fs from 'fs';
Code language: JavaScript (javascript)
We now create a file named demo.txt
and add some dummy content.
Codedamn is Awesome
Code language: Markdown (markdown)
We write the following code to read a file using the fs.readFileSync()
method.
const output = fs.readFileSync('./demo.txt');
console.log(output.toString()); // Convert buffer to a string
Code language: JavaScript (javascript)
On running the script, we can see the file’s contents logged onto the console.
fs.writeFileSync()
To write to a file synchronously, use the FS Module’s fs.writeFileSync()
method. It accepts two arguments: the file location and the data to write to the file. There is also the option to include a third argument that contains parameters that will affect the output, such as the encoding, mode, and flag parameters. If the specified file does not exist, fs.writeFileSync()
creates a new one.
Let us now modify the contents of our previously created demo.txt
file. We write the following code to alter the file contents using the fs.writeFileSync()
method.
fs.writeFileSync('./demo.txt', 'Hello from Codedamn!');
Code language: JavaScript (javascript)
We can see the file’s contents changed once we ran the script.
Asynchronous APIs
The FS Module includes a variety of asynchronous APIs for interacting with the file system. These APIs are asynchronous. That means file interactions, such as reading, writing, deleting, copying, and so on, are non-blocking. The file operations will not halt the main thread; execution will continue concurrently with file operations. Since these API methods are more effective and don’t block the main thread, we use them more frequently.
There are two types of asynchronous APIs provided by the FS Module: callback-based APIs and promise-based APIs. Let’s discuss both of them one by one.
Callback Based APIs
You can interact with the file system asynchronously using the callback API. Each callback API method accepts a callback function, which gets called after the operation gets finished.
We write the following code to read a file using the callback-based API.
fs.readFile('./demo.txt', function (error, data) {
if (error) return;
console.log(data.toString());
})
Code language: JavaScript (javascript)
Although it has downsides, this non-blocking method is often better suited for Node.js projects. Using callbacks in asynchronous programming frequently leads to callback hell. If we ignore how you arrange your code, you could probably end up with a complicated stack of nested callback functions that are challenging to understand and update.
Promise Based API
In JavaScript, we use Promises to handle asynchronous operations. They are simple to operate when working with several asynchronous activities where callbacks might lead to an anti-pattern called “Callback Hell.” The FS Module’s promise-based API enables you to create asynchronous code synchronously by leveraging JavaScript’s async/await syntax. It looks like our code gets executed synchronously, but it’s not.
To use the promise-based API of the FS Module, we will import the module from the fs/promises
path.
const fsPromise = require('fs/promises');
Code language: JavaScript (javascript)
We write the following code to read a file using the promise-based API.
const output = await fsPromise.readFile('./demo.txt');
console.log(output.toString()); // Convert buffer to a string
Code language: JavaScript (javascript)
Promises are the most straightforward method for dealing with asynchronous actions. They can easily manage several asynchronous processes and handle errors better than callbacks and events.
Bonus
Aside from reading, writing, and deleting methods, the FS Module provides other handy utilities. We’ll go through a couple of them here.
fs.watchFile()
We utilize the fs.watchFile()
function to monitor changes to a file. It takes two parameters: the file path and a callback function. When the file is changed, the callback function gets invoked.
fs.watchFile('./demo.txt', function () {
console.log('File changed!');
});
Code language: JavaScript (javascript)
fs.readdir()
We use the fs.readdir()
function to read the contents of a directory asynchronously. We may use it with both the Callback and Promise-based APIs. This function produces an array containing the names of all the files in the directory.
const dir = await fsPromise.readdir('./');
console.log(dir); // [ '.cdmrc', 'demo.txt', 'index.js' ]
Code language: JavaScript (javascript)
fs.stats()
The fs.stat()
function returns data about the specified file or directory. It produces a stats object with several attributes and methods for obtaining information about the file or directory.
const stats = await fsPromise.stat('./demo.txt');
console.log(stats);
Code language: JavaScript (javascript)
Conclusion
This article addressed the built-in FS Module in Node.js. We explored the many sorts of APIs it offers. We also looked at how to read a file into memory using these other APIs. Finally, we examined several useful utility functions provided by the FS Module.
Thank you so much for reading ?
Sharing is caring
Did you like what Varun Tiwari wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses:
130 students learning
Husein Nasser
Backend Web Development with Python
107 students learning
Piyush Garg
Master Node.JS in Hindi