memfs (3.5.3)
Published 2026-02-24 14:01:15 +00:00 by atheaadmin
Installation
registry=npm install memfs@3.5.3"memfs": "3.5.3"About this package
memfs
In-memory file-system with Node's fs API.
- Node's
fsAPI implemented, see old API Status, missing list, missingopendir - Stores files in memory, in
Buffers - Throws sameish* errors as Node.js
- Has concept of i-nodes
- Implements hard links
- Implements soft links (aka symlinks, symbolic links)
- Permissions may* be implemented in the future
- Can be used in browser, see
memfs-webpack
Install
npm install --save memfs
Usage
import { fs } from 'memfs';
fs.writeFileSync('/hello.txt', 'World!');
fs.readFileSync('/hello.txt', 'utf8'); // World!
Create a file system from a plain JSON:
import { fs, vol } from 'memfs';
const json = {
'./README.md': '1',
'./src/index.js': '2',
'./node_modules/debug/index.js': '3',
};
vol.fromJSON(json, '/app');
fs.readFileSync('/app/README.md', 'utf8'); // 1
vol.readFileSync('/app/src/index.js', 'utf8'); // 2
Export to JSON:
vol.writeFileSync('/script.sh', 'sudo rm -rf *');
vol.toJSON(); // {"/script.sh": "sudo rm -rf *"}
Use it for testing:
vol.writeFileSync('/foo', 'bar');
expect(vol.toJSON()).toEqual({ '/foo': 'bar' });
Create as many filesystem volumes as you need:
import { Volume } from 'memfs';
const vol = Volume.fromJSON({ '/foo': 'bar' });
vol.readFileSync('/foo'); // bar
const vol2 = Volume.fromJSON({ '/foo': 'bar 2' });
vol2.readFileSync('/foo'); // bar 2
Use memfs together with unionfs to create one filesystem
from your in-memory volumes and the real disk filesystem:
import * as fs from 'fs';
import { ufs } from 'unionfs';
ufs.use(fs).use(vol);
ufs.readFileSync('/foo'); // bar
Use fs-monkey to monkey-patch Node's require function:
import { patchRequire } from 'fs-monkey';
vol.writeFileSync('/index.js', 'console.log("hi world")');
patchRequire(vol);
require('/index'); // hi world
Docs
See also
spyfs- spies on filesystem actionsunionfs- creates a union of multiple filesystem volumeslinkfs- redirects filesystem pathsfs-monkey- monkey-patches Node'sfsmodule andrequirefunctionlibfs- real filesystem (that executes UNIX system calls) implemented in JavaScript
License
Unlicense - public domain.
Dependencies
Dependencies
| ID | Version |
|---|---|
| fs-monkey | ^1.0.4 |
Development Dependencies
| ID | Version |
|---|---|
| @semantic-release/changelog | ^6.0.1 |
| @semantic-release/git | ^10.0.1 |
| @semantic-release/npm | ^9.0.1 |
| @types/jest | ^27.5.2 |
| @types/node | ^10.17.60 |
| husky | ^8.0.1 |
| jest | ^28.1.1 |
| prettier | ^2.7.1 |
| pretty-quick | ^3.1.3 |
| rimraf | ^3.0.2 |
| semantic-release | ^19.0.3 |
| ts-jest | ^28.0.5 |
| ts-node | ^10.8.1 |
| tslint | ^5.20.1 |
| tslint-config-common | ^1.6.0 |
| typescript | ^4.7.4 |
Keywords
fs
filesystem
fs.js
memory-fs
memfs
file
file system
mount
memory
in-memory
virtual
test
testing
mock