Socket
Socket
Sign inDemoInstall

memfs

Package Overview
Dependencies
1
Maintainers
1
Versions
141
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    memfs

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


Version published
Weekly downloads
15M
decreased by-0.79%
Maintainers
1
Install size
1.17 MB
Created
Weekly downloads
 

Package description

What is memfs?

The memfs npm package is an in-memory filesystem that mimics the Node.js fs module. It allows you to create an ephemeral file system that resides entirely in memory, without touching the actual disk. This can be useful for testing, mocking, and various other scenarios where you don't want to perform I/O operations on the real file system.

What are memfs's main functionalities?

Creating and manipulating files

This feature allows you to create, read, and write files in memory as if you were using the native fs module.

const { Volume } = require('memfs');
const vol = new Volume();
vol.writeFileSync('/hello.txt', 'Hello, World!');
const content = vol.readFileSync('/hello.txt', 'utf8');
console.log(content); // Outputs: Hello, World!

Directory operations

This feature enables you to create directories, list their contents, and perform other directory-related operations, all in memory.

const { Volume } = require('memfs');
const vol = new Volume();
vol.mkdirSync('/mydir');
vol.writeFileSync('/mydir/file.txt', 'My file content');
const files = vol.readdirSync('/mydir');
console.log(files); // Outputs: ['file.txt']

Linking and symlinking

This feature allows you to create hard links and symbolic links, mimicking the behavior of links on a real file system.

const { Volume } = require('memfs');
const vol = new Volume();
vol.writeFileSync('/original.txt', 'Content of original');
vol.linkSync('/original.txt', '/link.txt');
vol.symlinkSync('/original.txt', '/symlink.txt');
const linkContent = vol.readFileSync('/link.txt', 'utf8');
const symlinkContent = vol.readlinkSync('/symlink.txt');
console.log(linkContent); // Outputs: Content of original
console.log(symlinkContent); // Outputs: /original.txt

File system watching

This feature provides the ability to watch for changes in the file system, similar to fs.watch in the native fs module.

const { Volume } = require('memfs');
const vol = new Volume();
const fs = vol.promises;

async function watchExample() {
  await fs.writeFile('/watched.txt', 'Initial content');
  fs.watch('/watched.txt', (eventType, filename) => {
    console.log(`Event type: ${eventType}; File: ${filename}`);
  });
  await fs.writeFile('/watched.txt', 'Updated content');
}

watchExample();

Other packages similar to memfs

Changelog

Source

4.7.5 (2024-02-17)

Bug Fixes

  • inline thingies dependency (#1004) (6fc340d)

Readme

Source

memfs

JavaScript file system utilities for Node.js and browser.

Install

npm i memfs

Docs

Demos

See also

  • unionfs - creates a union of multiple filesystem volumes
  • fs-monkey - monkey-patches Node's fs module and require function
  • linkfs - redirects filesystem paths
  • spyfs - spies on filesystem actions

License

Apache 2.0

Keywords

FAQs

Last updated on 17 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc