@cowasm/memfs

中文文档
3.5.1发布于 2022/10/26UnlicenseTypeScriptCommonJS近 30 天下载 2.9万

In-memory file-system with Node's fs API.

原始使用文档
文档目录6 节
  1. Upstream description
  2. Install
  3. Usage
  4. Docs
  5. See also
  6. License

memfs

This is a fork of https://www.npmjs.com/package/memfs, since there's a bunch of critical bugs and missing maintenance there, which might not be a priority for that project, but are definitely a priority for me (though I'm sending PR's). Upstream memfs has nearly 12 million downloads a week, so I can see why doing these changes would be really scary there!

Things I've done here include:

  • update to newest jest, and increase maxWorkers for testing; fix an issue with one test
  • update to newest typescript and node typings
  • update target to es2020 from es5; this does make code run more efficiently and is much easier to debug if you want to directly edit node_modules
  • fix the 13 security vulnerabilities revealed by npm audit
  • upgrade prettier and switch to using the defaults
  • switch to package-lock.json instead of yarn's lock
  • fix chmodSync bug
  • fix utimesSync bug
  • fix realpathSync bug

See https://github.com/sagemathinc/memfs-js


Upstream description

In-memory file-system with Node's fs API.

  • Node's fs API implemented, see old API Status, missing list, missing opendir
  • Stores files in memory, in Buffers
  • Throws sameish* errors as Node.js
  • Has concept of i-nodes
  • Implements hard links
  • Implements soft links (aka symlinks, symbolic links)
  • Permissions may* be implemented in the future
  • Can be used in browser, see memfs-webpack

Install

npm install --save memfs

Usage

import { fs } from "memfs";

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

Create a file system from a plain JSON:

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

Export to JSON:

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

Use it for testing:

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

Create as many filesystem volumes as you need:

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

Use memfs together with unionfs to create one filesystem from your in-memory volumes and the real disk filesystem:

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

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

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

Use fs-monkey to monkey-patch Node's require function:

import { patchRequire } from "fs-monkey";

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

Docs

See also

  • spyfs - spies on filesystem actions
  • unionfs - creates a union of multiple filesystem volumes
  • linkfs - redirects filesystem paths
  • fs-monkey - monkey-patches Node's fs module and require function
  • libfs - real filesystem (that executes UNIX system calls) implemented in JavaScript

License

Unlicense - public domain.

版本与发布记录

@cowasm/memfs 共发布 3 个版本,首次发布于 2022/10/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.5.1latest2022/10/264 年前
3.5.02022/10/264 年前
3.4.82022/10/264 年前

关于 @cowasm/memfs 的常见问题

@cowasm/memfs 是什么?

In-memory file-system with Node's fs API.

如何安装 @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 和提交记录判断。