基本用法


🌐 Basic usage

const vfs = require('node:vfs');

const myVfs = vfs.create();
myVfs.mkdirSync('/dir', { recursive: true });
myVfs.writeFileSync('/dir/hello.txt', 'Hello, VFS!');

console.log(myVfs.readFileSync('/dir/hello.txt', 'utf8')); // 'Hello, VFS!' 

vfs.create() 默认返回由 MemoryProvider 支持的 VirtualFileSystem 实例。该实例提供同步、基于回调和基于 Promise 的文件系统方法,这些方法的形式与 node:fs API 相同。所有路径都是 POSIX 风格且为绝对路径(以 / 开头)。

默认情况下,文件树对 VFS 实例是私有的。要通过全局 node:fs 模块、require() 和 import 公开它,调用 vfs.mount();再次分离时调用 vfs.unmount()(或者依赖 using 声明)。

🌐 By default, the file tree is private to the VFS instance. To expose it through the global node:fs module, require(), and import, call vfs.mount(); call vfs.unmount() (or rely on a using declaration) to detach again.