Files
ai-course/node_modules/fs-monkey/docs/api/patchRequire.md
KQL ce6aa207e9 fix: 修复图片路径以适配GitHub Pages base path
- 将所有图片路径从绝对路径改为使用 process.env.PUBLIC_URL
- 修复 HomePage.tsx 中所有图片引用
- 修复 CoursePage.tsx 中所有图片引用
- 确保图片在 GitHub Pages 上正确加载

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 09:24:45 +08:00

1.6 KiB

patchRequire(vol[, unixifyPaths[, Module]])

Patches Node's module module to use a given fs-like object vol for module loading.

  • vol - fs-like object
  • unixifyPaths (optional) - whether to convert Windows paths to unix style paths, defaults to false.
  • Module (optional) - a module to patch, defaults to require('module')

Monkey-patches the require function in Node, this way you can make Node.js to require modules from your custom filesystem.

It expects an object with three filesystem methods implemented that are needed for the require function to work.

let vol = {
    readFileSync: () => {},
    realpathSync: () => {},
    statSync: () => {},
};

If you want to make Node.js to require your files from memory, you don't need to implement those functions yourself, just use the memfs package:

import {vol} from 'memfs';
import {patchRequire} from 'fs-monkey';

vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
patchRequire(vol);
require('/foo/bar'); // obi trice

Now the require function will only load the files from the vol file system, but not from the actual filesystem on the disk.

If you want the require function to load modules from both file systems, use the unionfs package to combine both filesystems into a union:

import {vol} from 'memfs';
import {patchRequire} from 'fs-monkey';
import {ufs} from 'unionfs';
import * as fs from 'fs';

vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
ufs
    .use(vol)
    .use(fs);
patchRequire(ufs);
require('/foo/bar.js'); // obi trice