streamich/memfs


In-memory filesystem with Node's API

https://www.npmjs.com/package/memfs

License: Unlicense

Language: TypeScript

Keywords: filesystem, fs, in-memory, memfs, memory


memfs

In-memory file-system with Node's fs API.

  • Node's fs API implemented, see API Status
  • 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 actions
  • unionfs - creates a union of multiple filesystem volumes
  • linkfs - redirects filesystem paths
  • fs-monkey - monkey-patches Node's fs module and require function
  • libfs - real filesystem (that executes UNIX system calls) implemented in JavaScript

License

Unlicense - public domain.

Project Statistics

Sourcerank 15
Repository Size 2.41 MB
Stars 494
Forks 47
Watchers 7
Open issues 28
Dependencies 1,240
Contributors 23
Tags 37
Created
Last updated
Last pushed

Top Contributors See all

Renovate Bot Vadim Dalecky Gareth Jones Laurent Dupuy Semantic Release Bot Artem Sapegin dependabot[bot] wanyxspe Aram Drevekenin Garrett Serack Richard Samuelson JIACHENG9 Shachar Itzhaky Standa Opichal Adam Miskiewicz Kiril Knysh Reinaldo Arrosi Jonathan Neal Rodrigo Saboya Klaus Meinhardt

Packages Referencing this Repo

memfs
In-memory file-system with Node's fs API.
Latest release 3.0.4 - Updated - 494 stars
memfs-nodebowl
In-memory file-system with Node's fs API.
Latest release 2.16.1 - Published - 494 stars
@forabi/memfs
In-memory file-system with Node's fs API.
Latest release 2.7.2 - Published - 494 stars

Recent Tags See all

v3.0.4 January 15, 2020
v3.0.3 December 25, 2019
v3.0.2 December 25, 2019
v3.0.1 November 26, 2019
v3.0.0 November 26, 2019
v3.0.0 November 26, 2019
v2.17.1 November 26, 2019
v2.17.0 November 26, 2019
v2.16.0 November 26, 2019
2.16.1 October 30, 2019
2.16.0 October 25, 2019
v2.15.5 July 16, 2019
v2.15.4 June 01, 2019
v2.15.3 June 01, 2019
v2.15.2 February 16, 2019

Something wrong with this page? Make a suggestion

Last synced: 2019-12-25 14:49:40 UTC

Login to resync this repository