Last modified: Jul 16, 2025 By Alexander Williams

Install fs-extra in Node.js

The fs-extra module enhances Node.js file system operations. It adds useful methods not found in the native fs module. This guide covers installation and basic usage.

Prerequisites

Before installing fs-extra, ensure you have:

  • Node.js installed on your system
  • npm (Node Package Manager) ready to use
  • A Node.js project initialized

If you need help setting up Node.js, check our guide on installing Express for similar setup steps.

Install fs-extra

Install fs-extra using npm with this command:


npm install fs-extra

This adds the module to your project's node_modules folder. It also updates your package.json file.

Verify Installation

Check if installation worked by requiring it in your code:


const fs = require('fs-extra');
console.log('fs-extra loaded successfully');

Run the file with Node.js. You should see the success message.

Basic Usage Examples

fs-extra extends Node's native file system capabilities. Here are some common use cases.

Copy Files

The copy method copies files or directories:


const fs = require('fs-extra');

// Copy file
fs.copy('source.txt', 'destination.txt')
  .then(() => console.log('File copied!'))
  .catch(err => console.error(err));

Remove Directory

The remove method deletes directories recursively:


fs.remove('/path/to/dir')
  .then(() => console.log('Directory removed!'))
  .catch(err => console.error(err));

Ensure Directory

The ensureDir method creates a directory if it doesn't exist:


fs.ensureDir('new-directory')
  .then(() => console.log('Directory ready!'))
  .catch(err => console.error(err));

Error Handling

Always handle errors when working with file operations. fs-extra uses promises for async operations.

For synchronous methods, use try-catch blocks. Learn more about error handling in Node.js.

Comparison with Native fs

fs-extra includes all native fs methods plus extras:

  • Promise support by default
  • Recursive directory operations
  • Additional utility methods

For simpler projects, you might use the native fs module. For complex file operations, fs-extra saves time.

Common Issues

If you encounter problems:

  • Verify Node.js and npm versions
  • Check file permissions
  • Ensure proper error handling

For module not found errors, see our guide on fixing missing modules.

Conclusion

fs-extra is a powerful extension to Node.js file system operations. It simplifies complex tasks with intuitive methods.

Install it with npm and enjoy enhanced file management in your projects. Combine it with other modules like dotenv for complete project setups.