The Node.js File System (fs
) module is one of the most essential tools for backend developers. Whether you want to read, write, or delete files, understanding how fs
works is crucial. A fun and practical way to learn it is by building a Command Line Interface (CLI) File Manager. This blog will guide you through creating one from scratch, giving you hands-on experience with real-world file operations.
✨ Why Learn the File System Module?
The fs
module in Node.js lets you interact with the file system in both synchronous and asynchronous ways. You'll frequently use it in projects like:
- Logging systems
- Content management systems (CMS)
- File uploads/downloads
- Local data caching
Instead of reading boring docs, let’s build something useful!
📂 Project: CLI File Manager
Our CLI will let the user:
- Create a file
- Read a file
- Append to a file
- Delete a file
🔧 Step-by-Step Guide
1. Project Setup
Create a new folder and initialize it:
mkdir cli-file-manager
cd cli-file-manager
npm init -y
2. Create index.js
Now create a file named index.js
and add the following code:
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const showMenu = () => {
console.log('\nChoose an option:');
console.log('1. Create a file');
console.log('2. Read a file');
console.log('3. Append to a file');
console.log('4. Delete a file');
console.log('5. Exit');
};
const createFile = (filename) => {
rl.question('Enter content: ', (content) => {
fs.writeFile(filename, content, (err) => {
if (err) console.log('Error:', err);
else console.log('File created successfully!');
showMenu();
});
});
};
const readFile = (filename) => {
fs.readFile(filename, 'utf8', (err, data) => {
if (err) console.log('Error:', err);
else console.log('File content:', data);
showMenu();
});
};
const appendToFile = (filename) => {
rl.question('Enter content to append: ', (content) => {
fs.appendFile(filename, `\n${content}`, (err) => {
if (err) console.log('Error:', err);
else console.log('Content appended!');
showMenu();
});
});
};
const deleteFile = (filename) => {
rl.question('Are you sure? (yes/no): ', (answer) => {
if (answer.toLowerCase() === 'yes') {
fs.unlink(filename, (err) => {
if (err) console.log('Error:', err);
else console.log('File deleted!');
showMenu();
});
} else {
console.log('File not deleted.');
showMenu();
}
});
};
const handleChoice = () => {
rl.question('Enter your choice: ', (choice) => {
if (choice === '5') {
console.log('Exiting...');
rl.close();
return;
}
rl.question('Enter filename: ', (filename) => {
switch (choice) {
case '1':
createFile(filename);
break;
case '2':
readFile(filename);
break;
case '3':
appendToFile(filename);
break;
case '4':
deleteFile(filename);
break;
default:
console.log('Invalid choice.');
showMenu();
}
});
});
};
const startCLI = () => {
showMenu();
handleChoice();
};
startCLI();
📄 Run Your File Manager
node index.js
You'll see:
Choose an option:
1. Create a file
2. Read a file
3. Append to a file
4. Delete a file
5. Exit
Happy coding! ✨
0 Comments