fs-extra
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
graceful-fs | 4.2.11 | 9.57 kB | ISC | prod optional | |
jsonfile | 6.1.0 | 5.68 kB | MIT | prod | |
universalify | 2.0.1 | 2.02 kB | MIT | prod |
FS-Extra is a module that extends the native fs
module provided in Node.js. Offering additional functionalities not available in the built-in fs
, FS-Extra includes methods that support and enhance operations such as creating directories recursively (mkdirs, mkdirsSync), copying directories or files (copy, copySync), and removing directories and their contents (remove, removeSync). The biggest selling point for FS-Extra is that all these utility functions come with promise support out of the box, rendering third-party Promise
libraries unnecessary.
Here's how you can use FS-Extra in your Node.js project. First, you need to install the FS-Extra package using the Node Package Manager (NPM) by executing npm install fs-extra
in your terminal.
Next, replace your conventional fs
require statement with fs-extra:
const fs = require('fs-extra');
Or for clarity and maintaining distinction from 'fs', you can name your variable fse
:
const fse = require('fs-extra');
Here's a practical usage example. Let's say you want to copy a file to a new location asynchronously:
fs.copy('/tmp/myfile', '/tmp/mynewfile')
.then(() => console.log('File has been successfully copied!'))
.catch(err => console.error('An error occurred while copying file:', err));
If you want to do this synchronously, you can do so by doing:
try {
fs.copySync('/tmp/myfile', '/tmp/mynewfile');
console.log('File has been successfully copied!');
} catch (err) {
console.error('An error occurred while copying file:', err);
}
With a simple require statement, not only do you get access to all the original fs
functionalities, but also an enriched set of methods that result in cleaner, more maintainable code.
The official FS-Extra documentation is available on the GitHub repository's README file. It's accessible via https://github.com/jprichardson/node-fs-extra link. The documentation comprises detailed descriptions and usage examples for each method in the FS-Extra toolbox.