Introduction to fs-extra
fs-extra is a comprehensive, lightweight, and flexible file system library for Node.js. It extends the built-in fs
module, providing additional features and making file manipulation easier and more powerful. In this guide, we’ll explore dozens of useful APIs provided by fs-extra with code snippets and an example app.
Installation
npm install fs-extra
API Examples
Copy Files and Directories
The copy
method allows you to copy files or directories from one location to another:
const fs = require('fs-extra');
fs.copy('/tmp/myfile', '/tmp/mynewfile')
.then(() => console.log('Copy successful!'))
.catch(err => console.error(err));
Remove Files and Directories
The remove
method deletes files or directories:
fs.remove('/tmp/myfile')
.then(() => console.log('File deleted!'))
.catch(err => console.error(err));
Ensure File
The ensureFile
method creates a file if it doesn’t already exist:
fs.ensureFile('/tmp/this/path/does/not/exist/file.txt')
.then(() => console.log('File created!'))
.catch(err => console.error(err));
Ensure Directory
The ensureDir
method creates a directory if it doesn’t already exist:
fs.ensureDir('/tmp/this/path/does/not/exist')
.then(() => console.log('Directory created!'))
.catch(err => console.error(err));
Read JSON
The readJson
method reads a JSON file and parses its content:
fs.readJson('/tmp/data.json')
.then(data => console.log(data))
.catch(err => console.error(err));
Write JSON
The writeJson
method writes an object to a JSON file:
const data = { name: 'John', age: 30 };
fs.writeJson('/tmp/data.json', data)
.then(() => console.log('JSON written!'))
.catch(err => console.error(err));
Moving Files
The move
method moves a file or directory:
fs.move('/tmp/file', '/tmp/newfile')
.then(() => console.log('File moved!'))
.catch(err => console.error(err));
Emptying Directories
The emptyDir
method empties a directory, removing all its contents:
fs.emptyDir('/tmp/mydir')
.then(() => console.log('Directory emptied!'))
.catch(err => console.error(err));
Example Application
Let’s create an example Node.js application that uses several fs-extra
APIs:
const fs = require('fs-extra');
async function main() {
try {
// Ensure directory exists
await fs.ensureDir('/tmp/app');
// Write some data to a JSON file
const data = { name: 'MyApp', version: '1.0.0' };
await fs.writeJson('/tmp/app/config.json', data);
console.log('Config file created!');
// Copy the config file
await fs.copy('/tmp/app/config.json', '/tmp/app/config.copy.json');
console.log('Config file copied!');
// Read the copied JSON file
const copiedData = await fs.readJson('/tmp/app/config.copy.json');
console.log('Copied config data:', copiedData);
// Move the copied file
await fs.move('/tmp/app/config.copy.json', '/tmp/app/config.new.json');
console.log('Copied config file moved!');
// Empty the directory
await fs.emptyDir('/tmp/app');
console.log('App directory emptied!');
} catch (err) {
console.error(err);
}
}
main();
This example demonstrates various fs-extra
functionalities including creating directories, writing JSON files, copying files, reading JSON data, moving files, and emptying directories.
Using fs-extra
simplifies complex file system operations and enhances productivity when building Node.js applications.
Hash: baa7356b4f0c2575dee44bd5f6d501229f80ea3a4d3bcaa5e3d2a118df6bd4ee