Socket
Socket
Sign inDemoInstall

fs-extra

Package Overview
Dependencies
97
Maintainers
1
Versions
96
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fs-extra

fs-extra contains methods that aren't included in the vanilla Node.js fs package.


Version published
Maintainers
1
Install size
7.37 MB
Created

Package description

What is fs-extra?

The fs-extra package is a file system module for Node.js that extends the built-in 'fs' module. It provides additional methods and simplifies certain file operations, such as copying, moving, deleting files and directories, and more. It also adds promise support to the fs methods.

What are fs-extra's main functionalities?

Copying files and directories

This feature allows you to copy files and directories from one location to another. The method returns a promise that resolves when the operation is complete.

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

fs.copy('/path/to/source', '/path/to/dest')
  .then(() => console.log('Copy successful!'))
  .catch(err => console.error(err));

Moving files and directories

This feature enables you to move files and directories to a new location. Like copy, it returns a promise and provides a simple API for a task that would otherwise require multiple steps.

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

fs.move('/path/to/source', '/path/to/dest')
  .then(() => console.log('Move successful!'))
  .catch(err => console.error(err));

Removing files and directories

This feature is used to delete files and directories. It is a safer and more powerful alternative to the standard 'fs.unlink' and 'fs.rmdir' methods, as it can remove non-empty directories.

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

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

Reading and writing JSON files

This feature simplifies the process of reading and writing JSON files. It automatically handles stringifying objects when writing and parsing JSON data when reading.

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

const myData = { name: 'fs-extra' };
fs.writeJson('/path/to/file.json', myData)
  .then(() => console.log('Write successful!'))
  .catch(err => console.error(err));

Ensuring a file or directory exists

This feature checks if a file or directory exists, and if it does not, it is created. This is useful for making sure that a given file or directory is present before performing operations on it.

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

fs.ensureFile('/path/to/file.txt')
  .then(() => console.log('File exists!'))
  .catch(err => console.error(err));

Other packages similar to fs-extra

Readme

Source

Node.js: fs-extra

This module simply patches the Node.js 'fs' object with extra methods.

Installation

npm install fs-extra

Make sure that you run the test script to verify that it works on your system. Navigate to the directory for the module and run: make test

Usage

fs = require('fs-extra')

Note that you can still use all of the vanilla Node.js file methods.

Methods

fs.copyFileSync(srcFile, destFile) //synchronously copies a file
fs.copyFile(srcFile, destFile, function(err)) //asynchronously copies a file

fs.rmrfSync(dir) //recursively deletes directory, like rm -rf on Unix
fs.rmrf(dir, callback)  //asynchronous version as before

License

(The MIT License)

Copyright (c) 2011 JP Richardson

Keywords

FAQs

Last updated on 11 Jan 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc