# @cowasm/memfs 中文文档

规范地址：https://npmcn.com/package/%40cowasm/memfs/

版本：3\.5\.1
npm 元数据更新时间：2022-10-26T05:30:25.594Z
中文译文更新时间：2026-11-10T21:06:59.846Z
软件包许可：Unlicense

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cowasm/memfs
原文对照：https://npmcn.com/package/%40cowasm/memfs/?lang=original
项目主页：https://github.com/sagemathinc/memfs-js#readme
源码仓库：https://github.com/sagemathinc/memfs-js

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：158 kB，32 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>= 4\.0\.0
- 运行依赖：1 个
- 首次发布：2022-10-26T01:58:13.888Z
- 最新版本发布：2022-10-26T05:30:25.491Z
- 近 30 天下载量：29263（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：3（近 12 个月 0 个）

## 中文简介

与Node 的 fs API 的 IMMR 文件系统.

## 安装

```sh
npm install @cowasm/memfs
```

也可以使用：`pnpm add @cowasm/memfs`、`yarn add @cowasm/memfs`、`bun add @cowasm/memfs`

## README

# 分钟

这是叉子 [https://www.npmjs.com/package/memfs (中文(简体) )](https://www.npmjs.com/package/memfs),因为那里有一大堆关键的bug和缺失的维护,这也许不是该项目的优先,而是绝对是我的优先(尽管我正在发送PR's). 上流媒体每周下载近一千万次!

我所做的一切包括:

-   更新到最新笑话, 并增加用于测试的最大工人; 用一个测试解决一个问题
-   更新到最新的打字和节点打字
-   从 es5 更新到 es2020 的目标; 这确实使代码运行效率更高, 如果您想要直接编辑节点 模块, 调试则容易得多
-   弥补npm审计显示的13个安全弱点
-   升级并切换到使用默认值
-   切换到 package-lock.json 而不是纱线锁
-   修复 chmod同步错误
-   修复 utimes 同步错误
-   修复 realpath 同步错误

见 [https://github.com/sagemathinc/memfs-js (中文(简体) )](https://github.com/sagemathinc/memfs-js)

* * *

## 上游描述

模拟文件系统 [节点的 `fs` API 密码](https://nodejs.org/api/fs.html)。 。 。 .

-   节点的 `fs` 已执行 API,见 [_旧 API 状态_](https://npmcn.com/package/%40cowasm/memfs/docs/api-status.md), (中文(简体) ), [缺少列表](https://github.com/streamich/memfs/issues/735), (中文(简体) ), [缺少 `opendir`](https://github.com/streamich/memfs/issues/663)
-   存储内存中的文件, 以 `Buffer`编号
-   丢出与 Node.js 相同的错误
-   概念为 _i-节点_
-   执行 _硬链接_
-   执行 _软链接_ (aka 符号链接,符号链接)
-   今后可执行授权\*
-   可在浏览器中使用,参见 [`memfs-webpack`](https://github.com/streamich/memfs-webpack)

### 安装

```shell
npm install --save memfs
```

## 使用量

```js
import { fs } from "memfs";

fs.writeFileSync("/hello.txt", "World!");
fs.readFileSync("/hello.txt", "utf8"); // World!
```

从普通 JSON 创建文件系统 :

```js
import { fs, vol } from "memfs";

const json = {
  "./README.md": "1",
  "./src/index.js": "2",
  "./node_modules/debug/index.js": "3",
};
vol.fromJSON(json, "/app");

fs.readFileSync("/app/README.md", "utf8"); // 1
vol.readFileSync("/app/src/index.js", "utf8"); // 2
```

导出为 JSON :

```js
vol.writeFileSync("/script.sh", "sudo rm -rf *");
vol.toJSON(); // {"/script.sh": "sudo rm -rf *"}
```

用于测试 :

```js
vol.writeFileSync("/foo", "bar");
expect(vol.toJSON()).toEqual({ "/foo": "bar" });
```

创建所需的文件系统卷 :

```js
import { Volume } from "memfs";

const vol = Volume.fromJSON({ "/foo": "bar" });
vol.readFileSync("/foo"); // bar

const vol2 = Volume.fromJSON({ "/foo": "bar 2" });
vol2.readFileSync("/foo"); // bar 2
```

使用 `memfs` 合并 [`unionfs`](https://github.com/streamich/unionfs) 创建一个文件系统 从您的内存卷和真实的磁盘文件系统:

```js
import * as fs from "fs";
import { ufs } from "unionfs";

ufs.use(fs).use(vol);

ufs.readFileSync("/foo"); // bar
```

使用 [`fs-monkey`](https://github.com/streamich/fs-monkey) 到猴斑点的 `require` 函数 :

```js
import { patchRequire } from "fs-monkey";

vol.writeFileSync("/index.js", 'console.log("hi world")');
patchRequire(vol);
require("/index"); // hi world
```

## 文档

-   [参考文献](https://npmcn.com/package/%40cowasm/memfs/docs/reference.md)
-   [相对路径](https://npmcn.com/package/%40cowasm/memfs/docs/relative-paths.md)
-   [API 状态](https://npmcn.com/package/%40cowasm/memfs/docs/api-status.md)
-   [依赖关系](https://npmcn.com/package/%40cowasm/memfs/docs/dependencies.md)

## 另见

-   [`spyfs`](https://github.com/streamich/spyfs) - 监视文件系统行动
-   [`unionfs`](https://github.com/streamich/unionfs) - 建立一个由多个文件系统卷组成的联盟
-   [`linkfs`](https://github.com/streamich/linkfs) - 重定向文件系统路径
-   [`fs-monkey`](https://github.com/streamich/fs-monkey) - 猴帽节点的 `fs` 模块和 `require` 函数
-   [`libfs`](https://github.com/streamich/full-js/blob/master/src/lib/fs.ts) - 在 JavaScript 中执行真实文件系统(执行 UNIX 系统调用)

## 许可证

[取消许可证](https://npmcn.com/package/%40cowasm/memfs/LICENSE) - 公有领域.

## 常见问题

### @cowasm/memfs 是什么？

与Node 的 fs API 的 IMMR 文件系统\.

### 如何安装 @cowasm/memfs？

在项目目录运行 npm install @cowasm/memfs，或使用 pnpm add / yarn add / bun add。

### @cowasm/memfs 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @cowasm/memfs 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @cowasm/memfs 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>= 4\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @cowasm/memfs 可以商用吗？

@cowasm/memfs 使用 Unlicense 许可。作者将代码贡献到公共领域，可不受限制地使用。以上为概括，不构成法律意见，请以许可原文为准。

### @cowasm/memfs 的下载量有多少？

根据 npm 官方下载统计，@cowasm/memfs 在 2026/10/11 – 2026/11/09 这 30 天内被下载 29,263 次，日均约 975 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @cowasm/memfs 还在维护吗？

最新版本 3\.5\.1 发布于 2022/10/26（4 年前）；至今共发布 3 个版本，其中过去 12 个月发布了 0 个；首次发布于 2022/10/26。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

