Registry / testing / memfs
library0.1.8jsnpmunverified

memfs is a JavaScript library that provides an in-memory file system, designed to be highly compatible with Node.js's native `fs` module API and the browser's File System (Access) API. It serves as a robust tool for testing, mocking file system operations, and creating isolated virtual environments across both Node.js and modern browser runtimes. The current stable version is `4.57.2`. The project exhibits an active release cadence, with frequent patch and minor updates, as indicated by the rapid succession of releases between January and April 2026. Its key differentiators include comprehensive dual API compatibility, built-in TypeScript support, and helpful utilities like directory snapshots, making it a versatile solution for cross-environment file system virtualization.

npm install memfs
INSTALL
IMPORT
SIG · MEMFS
M
memfs
testingjavascriptv0.1.8
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fs
import { fs } from 'memfs';
const fs = require('memfs').fs;
Accesses the Node.js 'fs' compatible API, allowing methods like `writeFileSync` and `readdirSync`. For CommonJS, `require('memfs').fs` is the correct pattern.
fsa
import { fsa } from 'memfs';
const fsa = require('memfs').fsa;
Accesses the browser File System Access API compatible implementation, providing methods like `getRoot` and `getFileHandle`.
Volume
import { Volume } from 'memfs';
import Volume from 'memfs';
Imports the core `Volume` class, which represents an in-memory file system instance. Useful for creating multiple isolated file systems or advanced configurations, e.g., `new Volume()`. It's a named export.

Demonstrates basic file and directory operations using the Node.js `fs` compatible API provided by `memfs`, including writing, reading, creating directories, and listing contents within an in-memory file system.

import { fs } from 'memfs'; import path from 'path'; // Recommended for robust path handling // memfs exports an 'fs' object that mimics Node.js fs module. // For creating a new, isolated volume, you'd typically instantiate Volume: // const vol = Volume.fromJSON({ '/foo': 'bar' }); // However, 'fs' export provides a convenient default instance. const volFs = fs; // Write a file volFs.writeFileSync('/hello.txt', 'Hello, World!'); console.log('File written: /hello.txt'); // Read a file const content = volFs.readFileSync('/hello.txt', 'utf-8'); console.log('Content of /hello.txt:', content); // Create a directory volFs.mkdirSync('/mydir'); console.log('Directory created: /mydir'); // Create a nested file volFs.writeFileSync('/mydir/nested.txt', 'Nested content here.'); console.log('File written: /mydir/nested.txt'); // List directory contents console.log('Contents of /:', volFs.readdirSync('/')); console.log('Contents of /mydir:', volFs.readdirSync('/mydir')); // Example using path.join for cross-platform path construction const filePath = path.join('/', 'mydir', 'nested.txt'); console.log('Joined path:', filePath); console.log('Content via joined path:', volFs.readFileSync(filePath, 'utf-8'));
Debug
Known issues
gotchamemfs provides an *in-memory* file system that is entirely separate from your actual disk file system. Operations on `memfs` instances will not affect real files and vice-versa. Always remember you are working with a virtual environment.
fix
Ensure you are intentionally interacting with the `memfs` instance (e.g., `fs.writeFileSync`) and not accidentally with the native `require('fs')` module.
affects: >=1.0
gotchaWhile `memfs` mimics both synchronous and asynchronous Node.js `fs` APIs (e.g., `readFile` vs `readFileSync`), many of the 'asynchronous' operations in `memfs` are often implemented synchronously under the hood, wrapped in Promises or callbacks for API compatibility. This can lead to unexpected behavior in terms of event loop blocking if very large operations are performed.
fix
Profile your application if performance or event loop blocking becomes an issue with large `memfs` operations. Design your code to handle potentially synchronous execution, even for 'async' `memfs` methods.
affects: >=1.0
gotchaPrior to `v4.57.1`, calling `fileHandle.close()` after streaming data via a Node.js `pipeline` to a `memfs` `FileHandle` could incorrectly result in an `EBADF` (Bad file descriptor) error.
fix
Update `memfs` to `v4.57.1` or later to resolve the `EBADF` error when closing `FileHandle` instances after pipeline streaming.
affects: <4.57.1
gotchaIn versions prior to `v4.57.2`, the `fs.access` method in `memfs` might not have correctly followed symlinks when checking for the executable (X) flag, leading to incorrect permission evaluations.
fix
Upgrade `memfs` to `v4.57.2` or a newer version to ensure proper symlink following and accurate executable permission checks with `fs.access`.
affects: <4.57.2
gotchaPath normalization in `memfs` (and Node.js `fs`) can be a source of confusion. For example, leading `./` in relative glob patterns might not always behave as expected across different utility functions.
fix
Ensure consistent path resolution, ideally by always using absolute paths or `path.resolve`/`path.join`. Update to `v4.56.11` or later for improved handling of leading `./` in relative glob patterns.
affects: <4.56.11
Errors
Common errors & fixes
TypeError: fs.writeFileSync is not a function
Attempting to call `writeFileSync` on the `memfs` module root directly (e.g., `memfs.writeFileSync`) instead of on the destructured `fs` object or `Volume` instance.
fix
Ensure you import the `fs` object specifically: `import { fs } from 'memfs';` (ESM) or `const { fs } = require('memfs');` (CommonJS), then use `fs.writeFileSync(...)`.
ReferenceError: require is not defined
Using CommonJS `require()` syntax (e.g., `const { fs } = require('memfs');`) in a modern Node.js environment configured for ES Modules (e.g., `"type": "module"` in `package.json` or `.mjs` files).
fix
Switch to ES Module `import` syntax: `import { fs } from 'memfs';`. If you must use CommonJS, ensure your project is configured as `"type": "commonjs"` or use `.cjs` file extensions.
RangeError: Maximum call stack size exceeded
Operating on extremely large files or deeply nested directory structures in `memfs` can consume excessive memory or hit JavaScript engine recursion limits, especially with synchronous operations.
fix
Review the scale of data and directory depth. For very large datasets, consider chunking operations, simplifying directory structures, or opting for a real file system. `memfs` is optimized for speed and mockability, not necessarily for production-scale large data storage.
Upgrade
Version history
0.1.8latest on npm
Audit
Dependencies
tslibrequiredRequired for TypeScript helper functions at runtime, typically a peer dependency for libraries compiled with TypeScript.
Agent activity
3 hits · last 30 days
node
2
Amazon
1
Resources
memfs — npm install memfs · libregistry