Introduction to fs-extra
fs-extra is a popular Node.js library that extends the functionality of the built-in ‘fs’ module. It provides dozens of additional methods that make file operations easier and more powerful. In this guide, we’ll explore some of the most useful APIs provided by fs-extra, complete with code snippets to demonstrate their usage. We’ll also create a small app that utilizes these APIs to perform common file operations.
Installation
npm install fs-extra
API Examples
Copying Files
const fs = require('fs-extra');
fs.copy('/path/to/source/file', '/path/to/destination/file', err => {
if (err) return console.error(err);
console.log('File copied successfully!');
});
Removing Files
const fs = require('fs-extra');
fs.remove('/path/to/file', err => {
if (err) return console.error(err);
console.log('File removed successfully!');
});
Ensuring a Directory Exists
const fs = require('fs-extra');
fs.ensureDir('/path/to/directory', err => {
if (err) return console.error(err);
console.log('Directory ensured!');
});
Reading JSON Files
const fs = require('fs-extra');
fs.readJson('/path/to/file.json', (err, obj) => {
if (err) return console.error(err);
console.log(obj);
});
Writing JSON Files
const fs = require('fs-extra');
const obj = { name: 'John', age: 30 };
fs.writeJson('/path/to/file.json', obj, err => {
if (err) return console.error(err);
console.log('JSON file written successfully!');
});
Moving Files
const fs = require('fs-extra');
fs.move('/path/to/source/file', '/path/to/destination/file', err => {
if (err) return console.error(err);
console.log('File moved successfully!');
});
Checking if File or Directory Exists
const fs = require('fs-extra');
fs.pathExists('/path/to/fileOrDirectory', (err, exists) => {
if (err) return console.error(err);
console.log(exists ? 'Path exists!' : 'Path does not exist.');
});
App Example Using fs-extra
Let’s create a simple app that demonstrates the usage of some fs-extra methods. The app will do the following:
- Ensure a directory exists.
- Create a JSON file within that directory.
- Copy the JSON file to another location.
- Read the JSON file and log its contents.
- Remove the original directory.
const fs = require('fs-extra');
async function main() {
const dir = './exampleDir';
const file = `${dir}/example.json`;
const copyDestination = './copy/example.json';
try {
// Ensure a directory exists
await fs.ensureDir(dir);
console.log('Directory ensured!');
// Write a JSON file within the directory
await fs.writeJson(file, { name: 'John', age: 30 });
console.log('JSON file written successfully!');
// Copy the JSON file to another location
await fs.copy(file, copyDestination);
console.log('File copied successfully!');
// Read the JSON file and log its contents
const data = await fs.readJson(file);
console.log(data);
// Remove the original directory
await fs.remove(dir);
console.log('Directory removed successfully!');
} catch (err) {
console.error(err);
}
}
main();
With fs-extra, your file operations in Node.js become more efficient and less error-prone. Happy coding!
Hash: baa7356b4f0c2575dee44bd5f6d501229f80ea3a4d3bcaa5e3d2a118df6bd4ee