Bytes

Reading and Writing Files using fs Module in Node JS

Introduction

The ability to read and write files is a fundamental aspect of any programming language. In Node.js, the "fs" module provides an easy-to-use interface for working with the file system. This module allows developers to read from and write to files, as well as perform other file system operations such as creating directories and renaming files. In this topic, we will explore the basics of using the "fs" module to read and write files in Node.js.

We will cover common use cases, such as reading and writing text files, as well as more advanced scenarios, such as working with binary files and streams. Whether you're a beginner or an experienced Node.js developer, understanding how to work with files using the "fs" module is an essential skill. So, let's dive in and learn how to leverage the power of the "fs" module to build powerful and efficient file system operations in your Node.js applications.

Reading files using the fs module

Reading files in Node.js is a common operation when working with the file system. The "fs" module in Node.js provides an easy-to-use interface for reading files. In this topic, we will explore how to read files using the "fs" module.

To begin, we need to require the "fs" module in our code:

const fs = require('fs');

Once we have access to the "fs" module, we can use the "fs.readFile" method to read the contents of a file. The "readFile" method takes two arguments: the path to the file we want to read and a callback function that will be called with the contents of the file once it has been read.

Here's an example of using the "fs.readFile" method:

fs.readFile('example.txt', (err, data) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(data.toString());
});

In this example, we're reading the contents of a file called "example.txt". When the file has been read, the callback function will be called with two arguments: "err", which will contain an error object if an error occurred during the read operation, and "data", which will contain the contents of the file as a buffer.

We're using the "toString" method to convert the buffer to a string so we can print it to the console. That's it for reading files using the "fs" module in Node.js. As you can see, it's a simple and straightforward process.

Writing Files using the fs module

Writing files in Node.js is an essential operation when working with the file system. The "fs" module in Node.js provides an easy-to-use interface for writing files. In this topic, we will explore how to write files using the "fs" module.

To begin, we need to require the "fs" module in our code:

const fs = require('fs');

Once we have access to the "fs" module, we can use the "fs.writeFile" method to writing data to a file. The "writeFile" method takes three arguments: the path to the file we want to write, the data we want to write to the file, and a callback function that will be called once the data has been written to the file.

Here's an example of using the "fs.writeFile" method:

const data = 'Hello, World!';
fs.writeFile('example.txt', data, (err) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log('Data written to file');
});

In this example, we're writing the string "Hello, World!" to a file called "example.txt". When the data has been written to the file, the callback function will be called. If an error occurred during the write operation, the "err" argument of the callback function will contain an error object.

It's important to note that the "fs.writeFile" method overwrites the contents of the file if it already exists. If you want to append data to a file without overwriting its contents, you can use the "fs.appendFile" method instead which we will study next.

That's it for writing files using the "fs" module in Node.js. As you can see, it's a simple and straightforward process.

Conclusion

In conclusion, the "fs" module in Node.js provides an easy-to-use interface for reading and writing files. By requiring the "fs" module in our code, we can use the "fs.readFile" and "fs.writeFile" methods to read and write files, respectively. The "fs" module also provides synchronous equivalents for these methods, as well as methods for appending data to existing files which we will study further. Overall, the "fs" module is an essential tool for working with the file system in Node.js, and understanding how to read and write files using this module is a fundamental skill for any Node.js developer.

Module 3: Working with File SystemReading and Writing Files using fs Module in Node JS

Top Tutorials

Related Articles

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter