function
vfs.create
Convenience factory equivalent to new VirtualFileSystem(provider, options).
const vfs = require('node:vfs');
// Default in-memory provider
const memoryVfs = vfs.create();
// Explicit provider
const realVfs = vfs.create(new vfs.RealFSProvider('/tmp/sandbox'));@param provider
The provider to use. Default: new MemoryProvider().
Convenience factory equivalent to new VirtualFileSystem(provider, options).
const vfs = require('node:vfs');
// Default in-memory provider
const memoryVfs = vfs.create();
// Explicit provider
const realVfs = vfs.create(new vfs.RealFSProvider('/tmp/sandbox'));Referenced types
class VirtualProvider
The base class for all VFS providers. Subclasses implement the essential primitives (such as open, stat, readdir, mkdir, rmdir, unlink, rename, etc.) and inherit default implementations of the derived methods (such as readFile, writeFile, exists, copyFile, access, etc.).
interface VirtualFileSystemOptions
class VirtualFileSystem
A VirtualFileSystem wraps a VirtualProvider and exposes a node:fs-like API. Each instance maintains its own file tree.
- appendFileSync: (path: PathOrFileDescriptor, data: string | Uint8Array<ArrayBufferLike>, options?: WriteFileOptions) => void
- fstatSync: {(fd: number, options?: StatOptions & { bigint: false }) => Stats; (fd: number, options: StatOptions & { bigint: true }) => BigIntStats; (fd: number, options?: StatOptions) => Stats | BigIntStats}
- lstatSync: {(path: PathLike) => Stats; (path: PathLike, options?: StatOptions & { bigint: false; throwIfNoEntry: true }) => Stats; (path: PathLike, options: StatOptions & { bigint: true; throwIfNoEntry: true }) => BigIntStats; (path: PathLike, options: StatOptions & { bigint: false; throwIfNoEntry: false }) => undefined | Stats; (path: PathLike, options: StatOptions & { bigint: true; throwIfNoEntry: false }) => undefined | BigIntStats; (path: PathLike, options: StatOptions & { throwIfNoEntry: true }) => Stats | BigIntStats; (path: PathLike, options?: StatOptions) => undefined | Stats | BigIntStats}
- mkdirSync: {(path: PathLike, options: MakeDirectoryOptions & { recursive: true }) => undefined | string; (path: PathLike, options?: null | Mode | MakeDirectoryOptions & { recursive: false }) => void; (path: PathLike, options?: null | Mode | MakeDirectoryOptions) => undefined | string}
- mkdtempSync: {(prefix: string, options?: EncodingOption) => string; (prefix: string, options: BufferEncodingOption) => NonSharedBuffer; (prefix: string, options?: EncodingOption) => string | NonSharedBuffer}
- readonly promises: Pick<__module, 'open' | 'rename' | 'watch' | 'readFile' | 'writeFile' | 'stat' | 'lstat' | 'readdir' | 'realpath' | 'readlink' | 'access' | 'rm' | 'truncate' | 'link' | 'mkdtemp' | 'appendFile' | 'mkdir' | 'rmdir' | 'unlink' | 'copyFile' | 'symlink' | 'chmod' | 'chown' | 'lchown' | 'utimes' | 'lutimes' | 'lchmod'>
- readdirSync: {(path: PathLike, options?: null | BufferEncoding | { encoding: unknown; recursive: boolean; withFileTypes: false }) => string[]; (path: PathLike, options: 'buffer' | { encoding: 'buffer'; recursive: boolean; withFileTypes: false }) => NonSharedBuffer[]; (path: PathLike, options?: null | BufferEncoding | ObjectEncodingOptions & { recursive: boolean; withFileTypes: false }) => string[] | NonSharedBuffer[]; (path: PathLike, options: ObjectEncodingOptions & { recursive: boolean; withFileTypes: true }) => Dirent<string>[]; (path: PathLike, options: { encoding: 'buffer'; recursive: boolean; withFileTypes: true }) => Dirent<NonSharedBuffer>[]}
- readFileSync: {(path: PathOrFileDescriptor, options: ReadFileSyncOptionsWithBuffer<T>) => BufferView<T>; (path: PathOrFileDescriptor, options?: null | ReadFileSyncOptionsWithBufferEncoding) => NonSharedBuffer; (path: PathOrFileDescriptor, options: BufferEncoding | ReadFileSyncOptionsWithStringEncoding) => string; (path: PathOrFileDescriptor, options: ReadFileSyncOptions) => string | NonSharedBuffer}
- readlinkSync: {(path: PathLike, options?: EncodingOption) => string; (path: PathLike, options: BufferEncodingOption) => NonSharedBuffer; (path: PathLike, options?: EncodingOption) => string | NonSharedBuffer}
- readSync: {(fd: number, buffer: ArrayBufferView, offset: number, length: number, position?: null | ReadPosition) => number; (fd: number, buffer: ArrayBufferView, opts?: ReadOptions) => number}
- statSync: {(path: PathLike) => Stats; (path: PathLike, options?: StatOptions & { bigint: false; throwIfNoEntry: true }) => Stats; (path: PathLike, options: StatOptions & { bigint: true; throwIfNoEntry: true }) => BigIntStats; (path: PathLike, options: StatOptions & { bigint: false; throwIfNoEntry: false }) => undefined | Stats; (path: PathLike, options: StatOptions & { bigint: true; throwIfNoEntry: false }) => undefined | BigIntStats; (path: PathLike, options: StatOptions & { throwIfNoEntry: true }) => Stats | BigIntStats; (path: PathLike, options?: StatOptions) => undefined | Stats | BigIntStats}
- unwatchFile: {(filename: PathLike, listener?: StatsListener) => void; (filename: PathLike, listener?: BigIntStatsListener) => void}
- watch: {(filename: PathLike, options?: null | BufferEncoding | WatchOptionsWithStringEncoding, listener?: WatchListener<string>) => FSWatcher; (filename: PathLike, options: 'buffer' | WatchOptionsWithBufferEncoding, listener: WatchListener<NonSharedBuffer>) => FSWatcher; (filename: PathLike, options: null | BufferEncoding | 'buffer' | WatchOptions, listener: WatchListener<string | NonSharedBuffer>) => FSWatcher; (filename: PathLike, listener: WatchListener<string>) => FSWatcher}
- watchFile: {(filename: PathLike, options: undefined | WatchFileOptions & { bigint: false }, listener: StatsListener) => StatWatcher; (filename: PathLike, options: undefined | WatchFileOptions & { bigint: true }, listener: BigIntStatsListener) => StatWatcher; (filename: PathLike, listener: StatsListener) => StatWatcher}
- writeFileSync: (file: PathOrFileDescriptor, data: string | ArrayBufferView<ArrayBufferLike>, options?: WriteFileOptions) => void
- writeSync: {(fd: number, buffer: ArrayBufferView, offset?: null | number, length?: null | number, position?: null | number) => number; (fd: number, string: string, position?: null | number, encoding?: null | BufferEncoding) => number}