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.
mock-fs is a package that provides a mock file system for Node.js. It is similar to memfs in that it allows you to create an in-memory file system for testing purposes. However, mock-fs overrides the native fs module, while memfs provides a separate file system instance.
unionfs is a package that can combine multiple file systems into a single cohesive file system interface. It can be used with memfs to overlay an in-memory file system on top of the real file system, providing a way to mix real and virtual file system operations.
fs-mock is another in-memory file system module for Node.js. It offers similar functionality to memfs, allowing you to mock the fs module for testing. It differs in API and implementation details, and the choice between fs-mock and memfs may come down to personal preference or specific use-case requirements.
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
Dependencies
This package depends on the following Node modules: buffer, events,
streams, path.
It also uses process and setImmediate globals, but mocks them, if not
available.
Reference
vol vs fs
This package exports vol and fs objects which both can be used for
filesystem operations but are slightly different.
import {vol, fs} from'memfs';
vol is an instance of Volume constructor, it is the default volume created
for your convenience. fs is an fs-like object created from vol using
createFsFromVolume(vol), see reference below.
All contents of the fs object are also exported individually, so you can use
memfs just like you would use the fs module:
It is just a shorthand for vol.fromJSON, see below.
Volume instance vol
vol.fromJSON(json[, cwd])
Adds files from a flat json object to the volume vol. The cwd argument
is optional and is used to compute absolute file paths, if a file path is
given in a relative form.
Legacy method, which is just an alias for vol.fromJSON.
vol.toJSON([paths[, json[, isRelative]]])
Exports the whole contents of the volume recursively to a flat JSON object.
paths is an optional argument that specifies one or more paths to be exported.
If this argument is omitted, the whole volume is exported. paths can be
an array of paths. A path can be a string, Buffer or an URL object.
json is an optional object parameter which will be populated with the exported files.
isRelative is boolean that specifies if returned paths should be relative.
vol.mkdirp(path, callback)
Creates a directory tree recursively. path specifies a directory to
create and can be a string, Buffer, or an URL object. callback is
called on completion and may receive only one argument - an Error object.
vol.mkdirpSync(path)
A synchronous version of vol.mkdirp(). This method throws.
createFsFromVolume(vol)
Returns an fs-like object created from a Volume instance vol.
All of the Node's fs API is implemented.
Some error messages may be inaccurate. File permissions are currently not
implemented (you have access to any file), basically fs.access() is a no-op.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
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 0 open source maintainers collaborating on the project.
Package last updated on 14 Aug 2017
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.
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.