Big update!Introducing GitHub Bot Commands. Learn more
Socket
Log inBook a demo

memfs

Package Overview
Dependencies
1
Maintainers
1
Versions
105
Issues
File Explorer

Advanced tools

memfs

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

    3.4.12latest

Version published
Maintainers
1
Weekly downloads
11,260,430
decreased by-9.31%

Weekly downloads

Changelog

Source

3.4.12 (2022-11-19)

Bug Fixes

  • mkdir return value (#882) (125ad2e)

Readme

Source

memfs

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

  • Node's fs API implemented, see old API Status, missing list, missing opendir
  • 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.

Keywords

FAQs

What is memfs?

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

Is memfs popular?

The npm package memfs receives a total of 10,579,194 weekly downloads. As such, memfs popularity was classified as popular.

Is memfs well maintained?

We found that memfs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.

Last updated on 19 Nov 2022

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc