Horje
How to use the fs module in Node ?

The fs module in Node.js provides an interface for working with the file system. It allows you to perform various operations such as reading from and writing to files, manipulating directories, and handling file permissions.

What is the fs Module?

The fs module is a built-in module in Node.js that provides file system-related functionality. It allows you to interact with the file system in a non-blocking, asynchronous manner, making it well-suited for I/O operations in Node.js applications.

Why Use the fs Module?

The fs module is essential for performing file system operations in Node.js applications. Whether you need to read configuration files, write log files, or serve static assets, the fs module provides the necessary tools to interact with the file system efficiently.

How to Use the fs Module?

The fs module provides a wide range of functions for working with files and directories. Some of the most commonly used functions include:

  • fs.readFile(): Reads the contents of a file asynchronously.
  • fs.writeFile(): Writes data to a file asynchronously, replacing the file if it already exists.
  • fs.appendFile(): Appends data to a file asynchronously, creating the file if it does not exist.
  • fs.readdir(): Reads the contents of a directory asynchronously.
  • fs.stat(): Retrieves information about a file asynchronously, such as its size, permissions, and timestamps.
  • fs.unlink(): Deletes a file asynchronously.
  • fs.mkdir(): Creates a directory asynchronously.
  • fs.rmdir(): Removes a directory asynchronously.

Importing the fs Module

To use the fs module in your Node.js application, you need to import it first

const fs = require('fs');

Reading Files

Asynchronously

The fs.readFile() method reads the contents of a file asynchronously. It takes the file path, encoding (optional), and a callback function as arguments.

fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
console.log('File content:', data);
});

Synchronously

The fs.readFileSync() method reads the contents of a file synchronously. It blocks the execution until the file is read.

try {
const data = fs.readFileSync('example.txt', 'utf8');
console.log('File content:', data);
} catch (err) {
console.error('Error reading file:', err);
}

Renaming Files

Asynchronously

The fs.rename() method renames a file asynchronously.

fs.rename('example.txt', 'newname.txt', (err) => {
if (err) {
console.error('Error renaming file:', err);
return;
}
console.log('File renamed successfully');
});

Synchronously

The fs.renameSync() method renames a file synchronously.

try {
fs.renameSync('example.txt', 'newname.txt');
console.log('File renamed successfully');
} catch (err) {
console.error('Error renaming file:', err);
}

Example: In this example, fs.readFile() is used to asynchronously read the contents of a file named example.txt, while fs.writeFile() is used to asynchronously write data to the same file.

const fs = require('fs');

// Read from a file
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});

// Write to a file
fs.writeFile('example.txt', 'Hello, world!', (err) => {
if (err) throw err;
console.log('File written successfully');
});



Reffered: https://www.geeksforgeeks.org


Node.js

Related
What is Clustering in Node? What is Clustering in Node?
Explain the Concept of Child Processes in Node Explain the Concept of Child Processes in Node
How to handle authentication in Node? How to handle authentication in Node?
How to handle form data in Express ? How to handle form data in Express ?
How to handle asynchronous operations in Node ? How to handle asynchronous operations in Node ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13