fs-extra
Drop-in extension of the Node.js fs module adding copy, move, JSON helpers, and ensure-style directory creation.
Official Documentation ↗v11. Check the official docs for the latest API surface.Installation
npm install fs-extrapnpm add fs-extraMethods
No methods match this filter.
Drop-in extension of the Node.js fs module adding copy, move, JSON helpers, and ensure-style directory creation.
Overview
fs-extra augments the Node fs module with the operations scripts kept reimplementing: recursive copy with filters, ensure-style creation, atomic-ish JSON file handling, remove that tolerates missing paths, and move that works across filesystems.
Every method returns a promise and also accepts a callback, which made it the default filesystem toolbox for scaffolders and build scripts. It re-exports everything from fs, so a single import can serve both plain and extended calls.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
copy— Copies files or whole directory trees with an optional predicate to skip entries.ensureDir / ensureFile— Creates a directory tree, or a file with its parent directories, without failing when it already exists.readJson / writeJson— Reads and writes JSON files directly, creating parent directories on write and formatting with spaces.remove / move / emptyDir— Deletes paths recursively without try/catch on ENOENT, moves across devices, and empties directories keeping them.
Usage Notes
- Prefer ensureDir over mkdir try/catch; the intent is clearer and failures are real failures.
- Use filter on copy to exclude heavy folders rather than deleting after copying.
- writeJson with spaces keeps generated config reviewable in git diffs.
Common Pitfalls
- remove is recursive and unforgiving; resolve and assert the final path before calling it on anything derived from input.
- copy into an existing directory merges rather than replaces, which can leave stale files behind.
- readJson throws SyntaxError on malformed files; wrap startup config reads with actionable messages.
Errors
The following points are quoted verbatim from the official documentation (https://github.com/jprichardson/node-fs-extra). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Sync methods on the other hand will throw if an error occurs. (https://github.com/jprichardson/node-fs-extra)
- Windows If you run the tests on the Windows and receive a lot of symbolic link EPERM permission errors, it’s because on Windows you need elevated privilege to create symbolic links. (https://github.com/jprichardson/node-fs-extra)
Security notes
Quoted from the official documentation (https://github.com/jprichardson/node-fs-extra); this is not a complete threat model.
- Windows If you run the tests on the Windows and receive a lot of symbolic link EPERM permission errors, it’s because on Windows you need elevated privilege to create symbolic links. (https://github.com/jprichardson/node-fs-extra)
FAQ
- Is fs-extra still needed on modern Node?
- Node now ships fs.promises.cp, rm, and mkdir recursive, which covers many old gaps. fs-extra remains convenient for readJson, writeJson with formatting, move across devices, and ensure semantics, and it normalizes behavior across Node versions in older toolchains.
- Why did my copy fail with ENOTEMPTY?
- The destination existed with conflicting files. Decide explicitly: overwrite true replaces, errorOnExist true turns the conflict into an error, and otherwise copy merges trees and can collide on files.
- Are fse methods safe against path traversal?
- No. fs-extra operates on paths exactly as given, so user-supplied names must be sanitized and resolved against a base directory before any copy, move, or remove call.
Report a problem with this API docOpens a pre-filled issue on GitHub.