{"id":11297,"library":"memory-fs","title":"In-Memory Filesystem (memory-fs)","description":"memory-fs provides a simple, synchronous and asynchronous, in-memory filesystem abstraction, designed to hold data entirely within a JavaScript object. It mirrors a subset of the Node.js `fs` module API, making it suitable for scenarios where persistent disk I/O is undesirable or impractical, such as build tools, bundlers, and testing environments. The current stable version, 0.5.0, was released in 2017. While not actively developing new features, it is a critical, stable dependency for projects like Webpack, which relies on it for fast, transient asset compilation. Its primary differentiator is its complete in-memory operation and direct API compatibility with many standard `fs` methods, offering a performant alternative to disk-based operations for temporary file storage.","status":"maintenance","version":"0.5.0","language":"javascript","source_language":"en","source_url":"https://github.com/webpack/memory-fs","tags":["javascript","fs","memory"],"install":[{"cmd":"npm install memory-fs","lang":"bash","label":"npm"},{"cmd":"yarn add memory-fs","lang":"bash","label":"yarn"},{"cmd":"pnpm add memory-fs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only. Attempting to use ES module import syntax will result in errors.","wrong":"import MemoryFileSystem from 'memory-fs';","symbol":"MemoryFileSystem","correct":"const MemoryFileSystem = require('memory-fs');"},{"note":"The `require` call returns the constructor function, which must be instantiated to get a functional filesystem object.","wrong":"const fs = require('memory-fs'); // Does not return an instance","symbol":"fsInstance","correct":"const fs = new MemoryFileSystem();"}],"quickstart":{"code":"const MemoryFileSystem = require('memory-fs');\nconst fs = new MemoryFileSystem(); // Optionally pass an initial JavaScript object, e.g., { 'src': { 'main.js': 'console.log(\"Hello\");' } }\n\n// Create directories\nfs.mkdirpSync(\"/a/test/dir\");\nconsole.log('Created /a/test/dir');\n\n// Write and read a file\nconst filePath = \"/a/test/dir/file.txt\";\nfs.writeFileSync(filePath, \"Hello World from memory-fs\");\nconsole.log(`Content of ${filePath}: ${fs.readFileSync(filePath, 'utf8')}`);\n\n// Async file unlink\nfs.unlink(filePath, function(err) {\n  if (err) {\n    console.error(`Error unlinking file: ${err.message}`);\n    return;\n  }\n  console.log(`${filePath} unlinked successfully.`);\n});\n\n// List directory contents\nconsole.log(`Contents of /a/test: ${fs.readdirSync(\"/a/test\")}`);\n\n// Get file/directory stats\nconst stats = fs.statSync(\"/a/test/dir\");\nconsole.log(`/a/test/dir is a directory: ${stats.isDirectory()}`);\n\n// Clean up directory\nfs.rmdirSync(\"/a/test/dir\");\nconsole.log('Removed /a/test/dir');","lang":"javascript","description":"Demonstrates basic synchronous and asynchronous file operations (create, write, read, delete, list, stat) using the `memory-fs` API."},"warnings":[{"fix":"Ensure your Node.js environment matches the specified engine compatibility range or use a tool like `nvm` to manage Node.js versions.","message":"The `engines` field in package.json restricts compatible Node.js versions. Specifically, it requires Node.js `>=4.3.0 <5.0.0 || >=5.10`. Using unsupported Node.js versions may lead to unexpected behavior or installation failures.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Always check the `memory-fs` documentation or source code for method availability. Do not assume full `fs` compatibility.","message":"memory-fs provides only a subset of the standard Node.js `fs` module API. While it implements common methods like `readFile`, `writeFile`, `readdir`, and `stat`, many advanced or less frequently used `fs` methods are not available.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Use CommonJS `require()` syntax: `const MemoryFileSystem = require('memory-fs');`.","message":"This package is CommonJS-only and does not natively support ES module `import` syntax. Attempting to use `import` will result in a runtime error.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"After `require('memory-fs')`, always call `new MemoryFileSystem()` to get an instance, e.g., `const fs = new MemoryFileSystem();`.","message":"Unlike the native `fs` module, `memory-fs` requires explicit instantiation via `new MemoryFileSystem()`. Importing the package directly does not yield a functional filesystem object.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `const MemoryFileSystem = require('memory-fs');` directly assigns the constructor to `MemoryFileSystem` and then `const fs = new MemoryFileSystem();` is called.","cause":"Attempting to call `new` on the result of `require('memory-fs')` when the variable was incorrectly reassigned or handled.","error":"TypeError: MemoryFileSystem is not a constructor"},{"fix":"This package is CommonJS-only. For projects using ES modules, you might need to use a bundler (like Webpack) that handles CommonJS dependencies, or restructure your code to load this module in a CommonJS context.","cause":"Trying to use `require` in a file or project configured for ES modules (`\"type\": \"module\"` in package.json).","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"Verify that the specific `fs` method you need is supported by `memory-fs`. If not, you may need to reconsider its use for that particular operation or find an alternative library.","cause":"Attempting to call an `fs` module method that is not implemented by `memory-fs` (e.g., `fs.watch`, `fs.createReadStream`).","error":"TypeError: fs.someMethod is not a function"}],"ecosystem":"npm"}