A simple in-memory filesystem that holds data in a javascript object.
npx @tessl/cli install tessl/npm-memory-fs@0.5.0A simple in-memory filesystem that stores file and directory data in JavaScript objects. It provides a familiar Node.js fs-like API with both synchronous and asynchronous methods for common filesystem operations. Designed for scenarios where temporary file storage is needed without touching the actual filesystem.
npm install memory-fsconst MemoryFileSystem = require("memory-fs");const MemoryFileSystem = require("memory-fs");
// Create filesystem instance
const fs = new MemoryFileSystem(); // Optionally pass existing data object
// Create directories
fs.mkdirpSync("/a/test/dir");
// Write files
fs.writeFileSync("/a/test/dir/file.txt", "Hello World");
// Read files
const content = fs.readFileSync("/a/test/dir/file.txt"); // Returns Buffer
const text = fs.readFileSync("/a/test/dir/file.txt", "utf-8"); // Returns string
// List directory contents
fs.readdirSync("/a/test"); // Returns ["dir"]
// Check file/directory stats
fs.statSync("/a/test/dir").isDirectory(); // Returns true
// Remove files and directories
fs.unlinkSync("/a/test/dir/file.txt");
fs.rmdirSync("/a/test/dir");
// Cross-platform path support
fs.mkdirpSync("C:\\\\use\\\\windows\\\\style\\\\paths");Create a new in-memory filesystem instance.
/**
* Create a new MemoryFileSystem instance
* @param {Object} [data] - Optional initial data object to populate filesystem
*/
class MemoryFileSystem {
constructor(data);
}Check if files or directories exist and get their metadata.
/**
* Get internal metadata for a file or directory (synchronous)
* @param {string} path - File or directory path
* @returns {Object|undefined} Internal metadata object or undefined if not found
*/
meta(path);
/**
* Check if a file or directory exists (synchronous)
* @param {string} path - File or directory path
* @returns {boolean} True if exists, false otherwise
*/
existsSync(path);
/**
* Check if a file or directory exists (asynchronous)
* @param {string} path - File or directory path
* @param {function} callback - Callback function receiving (exists)
*/
exists(path, callback);
/**
* Get file or directory statistics (synchronous)
* @param {string} path - File or directory path
* @returns {Object} Stat object with isFile(), isDirectory(), etc. methods
*/
statSync(path);
/**
* Get file or directory statistics (asynchronous)
* @param {string} path - File or directory path
* @param {function} callback - Callback function receiving (err, stats)
*/
stat(path, callback);Read and write file contents with various encoding options.
/**
* Read file contents (synchronous)
* @param {string} path - File path
* @param {string|Object} [optionsOrEncoding] - Encoding string or options object
* @returns {Buffer|string} File contents as Buffer or string if encoding specified
*/
readFileSync(path, optionsOrEncoding);
/**
* Read file contents (asynchronous)
* @param {string} path - File path
* @param {string|Object} [encoding] - Encoding string or options object
* @param {function} callback - Callback function receiving (err, data)
*/
readFile(path, encoding, callback);
/**
* Write file contents (synchronous)
* @param {string} path - File path
* @param {string|Buffer} content - Content to write
* @param {string|Object} [optionsOrEncoding] - Encoding string or options object
*/
writeFileSync(path, content, optionsOrEncoding);
/**
* Write file contents (asynchronous)
* @param {string} path - File path
* @param {string|Buffer} content - Content to write
* @param {string|Object} [encoding] - Optional encoding string or options object
* @param {function} callback - Callback function receiving (err)
*/
writeFile(path, content, [encoding], callback);
/**
* Remove a file (synchronous)
* @param {string} path - File path
*/
unlinkSync(path);
/**
* Remove a file (asynchronous)
* @param {string} path - File path
* @param {function} callback - Callback function receiving (err)
*/
unlink(path, callback);Create, read, and remove directories with full cross-platform path support.
/**
* Read directory contents (synchronous)
* @param {string} path - Directory path
* @returns {string[]} Array of file and directory names
*/
readdirSync(path);
/**
* Read directory contents (asynchronous)
* @param {string} path - Directory path
* @param {function} callback - Callback function receiving (err, files)
*/
readdir(path, callback);
/**
* Create a single directory (synchronous)
* @param {string} path - Directory path
* @throws {Error} If parent directory doesn't exist
*/
mkdirSync(path);
/**
* Create a single directory (asynchronous)
* @param {string} path - Directory path
* @param {Object} [options] - Optional options object
* @param {function} callback - Callback function receiving (err)
*/
mkdir(path, [options], callback);
/**
* Create directory recursively (synchronous)
* @param {string} path - Directory path
*/
mkdirpSync(path);
/**
* Create directory recursively (asynchronous)
* @param {string} path - Directory path
* @param {function} callback - Callback function receiving (err)
*/
mkdirp(path, callback);
/**
* Remove an empty directory (synchronous)
* @param {string} path - Directory path
*/
rmdirSync(path);
/**
* Remove an empty directory (asynchronous)
* @param {string} path - Directory path
* @param {function} callback - Callback function receiving (err)
*/
rmdir(path, callback);Create readable and writable streams for file operations.
/**
* Create a readable stream for a file
* @param {string} path - File path
* @param {Object} [options] - Stream options with start/end properties
* @returns {ReadableStream} Readable stream for the file
*/
createReadStream(path, options);
/**
* Create a writable stream for a file
* @param {string} path - File path
* @returns {WritableStream} Writable stream for the file
*/
createWriteStream(path);Path manipulation utilities available as instance properties.
/**
* Join path components with proper separator handling
* @param {string} path - Base path
* @param {string} request - Path component to join
* @returns {string} Joined and normalized path
*/
fs.join(path, request);
/**
* Normalize path by resolving . and .. components
* @param {string} path - Path to normalize
* @returns {string} Normalized path
*/
fs.normalize(path);
/**
* Convert path string to array of components
* @param {string} path - Path to convert
* @returns {string[]} Array of path components
*/
fs.pathToArray(path);Limited symbolic link support (readlink throws ENOSYS).
/**
* Read symbolic link target (synchronous) - NOT IMPLEMENTED
* @param {string} path - Symbolic link path
* @throws {Error} Always throws ENOSYS error (not implemented)
*/
readlinkSync(path);
/**
* Read symbolic link target (asynchronous) - NOT IMPLEMENTED
* @param {string} path - Symbolic link path
* @param {function} callback - Callback function receiving (err, linkString)
*/
readlink(path, callback);/**
* File/directory statistics object returned by statSync() and stat()
*/
interface StatObject {
isFile(): boolean; // Returns true if path is a file
isDirectory(): boolean; // Returns true if path is a directory
isBlockDevice(): boolean; // Always returns false
isCharacterDevice(): boolean; // Always returns false
isSymbolicLink(): boolean; // Always returns false
isFIFO(): boolean; // Always returns false
isSocket(): boolean; // Always returns false
}/**
* Readable stream created by createReadStream()
*/
interface ReadableStream {
// Standard Node.js ReadableStream interface
on(event: string, listener: function): ReadableStream;
pipe(destination: WritableStream): WritableStream;
}
/**
* Writable stream created by createWriteStream()
*/
interface WritableStream {
// Standard Node.js WritableStream interface
write(chunk: string|Buffer, encoding?: string, callback?: function): boolean;
end(chunk?: string|Buffer, encoding?: string, callback?: function): void;
}memory-fs uses errno-compatible error codes through the MemoryFileSystemError class:
/**
* Custom error class for filesystem operations
* @param {Object} err - Error object with code and description
* @param {string} path - File path that caused the error
* @param {string} operation - Operation that failed
*/
class MemoryFileSystemError extends Error {
constructor(err, path, operation);
name: string; // Error name ("MemoryFileSystemError")
message: string; // Formatted error message
code: string; // Error code (ENOENT, EISDIR, ENOTDIR, etc.)
errno: number; // Error number
path: string; // Path that caused the error
operation: string; // Operation that failed
}Common Error Codes:
ENOENT: File or directory does not existEISDIR: Operation expected a file but found a directoryENOTDIR: Operation expected a directory but found a fileEEXIST: File or directory already existsEPERM: Operation not permittedENOSYS: Function not implemented (readlink operations)/path/to/file) and Windows (C:\\path\\to\\file) style pathsmkdirpSync() creates parent directories automatically