A VirtualFileSystem wraps a VirtualProvider and exposes a
node:fs-like API. Each instance maintains its own file tree.
new VirtualFileSystem(provider?, options?): VirtualFileSystem
VirtualProvidernew MemoryProvider().vfs.mount(): string
stringMounts the virtual file system and returns the resulting mount point.
After mounting, files in the VFS can be accessed through the
node:fs module and resolved through require() and import
using paths under the returned mount point.
Mount points always live inside a reserved namespace that cannot have child file system entries,
so virtual paths never conflate with (or shadow) real paths. The virtual path scheme is subject to
change and users should not manually construct them based on assumptions. Instead, obtain
them from what vfs.mount() returns or vfs.mountPoint.
const vfs = require('node:vfs'); const fs = require('node:fs'); const myVfs = vfs.create(); myVfs.writeFileSync('/data.txt', 'Hello'); const mountPoint = myVfs.mount(); // e.g. '/dev/null/vfs/0' fs.readFileSync(`${mountPoint}/data.txt`, 'utf8'); // 'Hello'
Each VirtualFileSystem instance may be mounted at most once at a
time. Attempting to mount an already-mounted instance throws
ERR_INVALID_STATE. Because each instance mounts inside its own
per-layer namespace, mounts from different instances can never
overlap.
The VFS supports the Explicit Resource Management proposal. Use
a using declaration to unmount automatically when leaving scope:
const vfs = require('node:vfs'); const fs = require('node:fs'); let mountPoint; { using myVfs = vfs.create(); myVfs.writeFileSync('/data.txt', 'Hello'); mountPoint = myVfs.mount(); fs.readFileSync(`${mountPoint}/data.txt`, 'utf8'); // 'Hello' } // VFS is automatically unmounted here fs.existsSync(`${mountPoint}/data.txt`); // false
vfs.unmount(): void
Unmounts the virtual file system. After unmounting, virtual files
are no longer reachable through node:fs, require(), or import.
The same instance may be mounted again by calling mount().
This method is idempotent: calling unmount() on a VFS that is not
currently mounted has no effect.
true while the VFS is mounted; false otherwise.
The current mount point as an absolute string (the value returned by
the last vfs.mount() call), or null when the VFS is not
mounted.
The current mount point as a file: URL string (the vfs.mountPoint
path converted with url.pathToFileURL()), or null when the VFS
is not mounted.
This is a convenience for addressing mounted files with URL-based
APIs such as dynamic import():
import vfs from 'node:vfs'; const myVfs = vfs.create(); myVfs.writeFileSync('/mod.mjs', 'export const value = 42;'); myVfs.mount(); const { value } = await import(`${myVfs.mountPointURL}/mod.mjs`); console.log(value); // 42 myVfs.unmount();
The provider backing this VFS instance.
true when the underlying provider is read-only.
VirtualFileSystem implements the following methods, with the same
signatures as their node:fs counterparts:
existsSync(path)statSync(path[, options])lstatSync(path[, options])readFileSync(path[, options])writeFileSync(path, data[, options])appendFileSync(path, data[, options])readdirSync(path[, options])mkdirSync(path[, options])rmdirSync(path)unlinkSync(path)renameSync(oldPath, newPath)copyFileSync(src, dest[, mode])realpathSync(path[, options])readlinkSync(path[, options])symlinkSync(target, path[, type])accessSync(path[, mode])rmSync(path[, options])truncateSync(path[, len])ftruncateSync(fd[, len])linkSync(existingPath, newPath)chmodSync(path, mode)chownSync(path, uid, gid)lchownSync(path, uid, gid)utimesSync(path, atime, mtime)lutimesSync(path, atime, mtime)mkdtempSync(prefix)opendirSync(path[, options])openAsBlob(path[, options])- File-descriptor ops:
openSync,closeSync,readSync,writeSync,fstatSync - Streams:
createReadStream,createWriteStream - Watchers:
watch,watchFile,unwatchFile
readFile, writeFile, stat, lstat, readdir, realpath, readlink,
access, open, close, read, write, rm, fstat, truncate,
ftruncate, link, mkdtemp, opendir. Each takes a Node.js-style
callback (err, ...result) => {}.
vfs.promises exposes the promise-based variants:
const vfs = require('node:vfs'); async function example() { const myVfs = vfs.create(); await myVfs.promises.writeFile('/file.txt', 'hello'); const data = await myVfs.promises.readFile('/file.txt', 'utf8'); return data; } example();
The promise namespace mirrors fs.promises and includes readFile,
writeFile, appendFile, stat, lstat, readdir, mkdir, rmdir,
unlink, rename, copyFile, realpath, readlink, symlink,
access, rm, truncate, link, mkdtemp, chmod, chown, lchown,
utimes, lutimes, open, lchmod, and watch.