memfs is an in-memory file system. It implements two APIs:
fs module --- a drop-in
replacement you can use anywhere the fs module is expected (tests, mocks,
bundlers, sandboxes). Files live in memory instead of on disk.FileSystemDirectoryHandle interface a browser exposes, backed by memory.It also ships adapters that translate between those two APIs, so you can run fs-based
code in the browser on top of a real directory, or expose an fs-like filesystem through
FSA handles. It runs in Node.js, Deno, Bun, and browsers, and stores file contents
in Uint8Array buffers.
import { fs } from 'memfs';
fs.writeFileSync('/hello.txt', 'Hello, World!');
fs.readFileSync('/hello.txt', 'utf8'); // 'Hello, World!'
fs.mkdirSync('/dir');
fs.readdirSync('/'); // ['dir', 'hello.txt']
npm install memfs
The default export gives you a ready-to-use filesystem. fs is a bound, require('fs')-shaped
object --- vol is the Volume behind it. They share the same storage.
import { fs, vol } from 'memfs';
fs.writeFileSync('/script.sh', 'sudo rm -rf *');
vol.toJSON(); // {'/script.sh': 'sudo rm -rf *'}
For an isolated filesystem (recommended for tests, so volumes never bleed into
each other), call memfs() --- it returns a fresh fs/vol pair, optionally
seeded from a JSON tree:
import { memfs } from 'memfs';
const { fs, vol } = memfs({ '/app/index.js': 'console.log(1)' });
fs.readFileSync('/app/index.js', 'utf8'); // 'console.log(1)'
| Page | Import | What it covers |
|---|---|---|
| Node fs API | memfs |
The fs/vol/Volume/memfs() model and the full Node fs method surface |
| Volumes | memfs |
Building volumes from JSON, exporting them, and unionfs / fs-monkey integration |
| File System Access | memfs/lib/fsa |
An in-memory FSA filesystem (FileSystemDirectoryHandle and friends) |
| Adapters | memfs/lib/node-to-fsa, memfs/lib/fsa-to-node |
Convert between the fs API and the FSA API in either direction |
| Snapshots | @jsonjoy.com/fs-snapshot |
POJO / CBOR / JSON snapshots of any fs directory |
| Tree printing | @jsonjoy.com/fs-print |
Print an ASCII tree of any fs directory |
`memfs` is built from a set of smaller `@jsonjoy.com/fs-*` packages
(`fs-node`, `fs-fsa`, `fs-node-to-fsa`, `fs-fsa-to-node`, `fs-snapshot`,
`fs-print`, ...). Installing `memfs` pulls them all in; the snapshot and
tree-printing utilities are also publishable on their own.