Modules fs in node js
Understanding the fs
Module in Node.js
The fs
module in Node.js is a built-in module that provides an API for interacting with the file system. It allows you to perform various operations like reading, writing, updating, and deleting files and directories.
Key Features of the fs
Module
- Reading Files: Read the content of a file.
- Writing Files: Write data to a file.
- Appending Files: Append data to an existing file.
- Deleting Files: Remove files from the system.
- Directory Operations: Create, read, and delete directories.
Installation with npm
In earlier versions of Node.js, you would typically install modules via npm, including built-in ones like fs
. However, fs
is a core module, so you don't need to install it separately. The command npm install fs --save
is outdated for fs
as it’s already included in Node.js by default.
Example 1: Reading a File
Let's start with reading a file using the fs
module.
Code:
const fs = require('fs');
// Reading a file asynchronously
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
console.log('File content:', data);
});
Explanation:
fs.readFile
reads the content of the file asynchronously.'example.txt'
is the file you want to read.'utf8'
specifies the encoding.- The callback function receives an error object
err
(if any) and the file contentdata
.
Output:
If the file example.txt
contains:
Hello, Node.js!
The output in the console will be:
File content: Hello, Node.js!
Example 2: Writing to a File
Next, let’s write some data to a file.
Code:
const fs = require('fs');
// Writing to a file asynchronously
fs.writeFile('output.txt', 'Hello, Node.js!', 'utf8', (err) => {
if (err) {
console.error('Error writing to the file:', err);
return;
}
console.log('File has been written successfully!');
});
Explanation:
fs.writeFile
writes data to a file asynchronously.'output.txt'
is the file where the content will be written.'Hello, Node.js!'
is the content to be written.'utf8'
specifies the encoding.
Output:
A file named output.txt
will be created with the content:
Hello, Node.js!
Console output:
File has been written successfully!
Example 3: Appending to a File
Appending data to an existing file is also straightforward.
Code:
const fs = require('fs');
// Appending to a file asynchronously
fs.appendFile('output.txt', '\nThis is additional content.', 'utf8', (err) => {
if (err) {
console.error('Error appending to the file:', err);
return;
}
console.log('Content has been appended successfully!');
});
Explanation:
fs.appendFile
adds data to an existing file.- The content
\nThis is additional content.
will be added at the end of the fileoutput.txt
.
Output:
If output.txt
initially contained:
Hello, Node.js!
After running the code, it will contain:
Hello, Node.js!
This is additional content.
Console output:
Content has been appended successfully!
Example 4: Deleting a File
Deleting a file is just as easy.
Code:
const fs = require('fs');
// Deleting a file asynchronously
fs.unlink('output.txt', (err) => {
if (err) {
console.error('Error deleting the file:', err);
return;
}
console.log('File has been deleted successfully!');
});
Explanation:
fs.unlink
is used to delete a file.'output.txt'
is the file to be deleted.
Output:
If output.txt
exists, it will be deleted, and the console will show:
File has been deleted successfully!
Example 5: Working with Directories
You can also create and remove directories using the fs
module.
Creating a Directory:
const fs = require('fs');
// Creating a new directory
fs.mkdir('new_directory', { recursive: true }, (err) => {
if (err) {
console.error('Error creating directory:', err);
return;
}
console.log('Directory created successfully!');
});
Deleting a Directory:
const fs = require('fs');
// Deleting a directory
fs.rmdir('new_directory', { recursive: true }, (err) => {
if (err) {
console.error('Error deleting directory:', err);
return;
}
console.log('Directory deleted successfully!');
});
Conclusion
The fs
module is powerful and essential for file system operations in Node.js. With it, you can easily manage files and directories within your application. Remember, since fs
is a core module, there’s no need to install it separately using npm.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.