An in-memory filesystem implementation for Cloudflare Workers. Can be mounted via worker-fs-mount to provide ephemeral filesystem storage that persists within a single Worker instance.
npm install memory-fs worker-fs-mount[alias]
"node:fs/promises" = "worker-fs-mount/fs"import { exports } from 'cloudflare:workers';
import { MemoryFilesystem } from 'memory-fs';
import { mount } from 'worker-fs-mount';
import fs from 'node:fs/promises';
// Re-export to make available via exports
export { MemoryFilesystem };
// Mount at module level using importable exports
mount('/mem', exports.MemoryFilesystem);
export default {
async fetch(request: Request): Promise<Response> {
// Use standard fs operations
await fs.writeFile('/mem/hello.txt', 'Hello, World!');
const content = await fs.readFile('/mem/hello.txt', 'utf8');
// Create directories
await fs.mkdir('/mem/projects/my-app', { recursive: true });
// List directory contents
const files = await fs.readdir('/mem/projects');
return new Response(content);
}
}- Full
WorkerFilesysteminterface implementation - Zero external dependencies (pure in-memory Map storage)
- Support for files, directories, and symlinks
- Streaming read/write support
- Shared state across requests (within same isolate)
- Reset functionality for testing
The MemoryFilesystem class extends WorkerEntrypoint and implements the full WorkerFilesystem interface. See the worker-fs-mount README for the complete API reference.
Additional method:
MemoryFilesystem.resetState()- Reset the shared filesystem state to empty (useful for testing)
Data is stored in a shared JavaScript Map<string, FsNode> where:
- Keys are normalized absolute paths (e.g.,
/foo/bar.txt) - Values are node objects containing type, content (for files), and metadata
- State is shared across all instances within the same isolate
- Testing: Fast, isolated filesystem for unit tests
- Caching: Temporary file cache within a request or session
- Scratch space: Working directory for file processing pipelines
- Development: Local development without external storage dependencies
| Feature | memory-fs | durable-object-fs | r2-fs |
|---|---|---|---|
| Persistence | Within isolate | Permanent | Permanent |
| Max file size | Memory limited | ~100MB | 5GB |
| Latency | Instant | Low | Higher |
| Cost | Free (memory) | DO pricing | R2 pricing |
| Use case | Testing, temp files | Small persistent files | Large files |
- Ephemeral: Data is lost when the Worker isolate is recycled
- Memory bound: Total storage limited by Worker memory limits
- Single isolate: State is not shared across different Worker instances
- No durability: Not suitable for data that must survive restarts
MIT