Skip to content

Instantly share code, notes, and snippets.

@forivall
Last active September 13, 2025 01:50
Show Gist options
  • Save forivall/021f5642a370af4ce30364b7bd6419fd to your computer and use it in GitHub Desktop.
Save forivall/021f5642a370af4ce30364b7bd6419fd to your computer and use it in GitHub Desktop.
// manually loading a module with only public apis
type ModuleBody = (
exports: object,
require: NodeRequire,
module: Module,
filename: string,
dirname: string,
) => void;
const moduleSource = wrap(fs.readFileSync(filename, 'utf8'));
const moduleBody: ModuleBody = runInThisContext(moduleSource, { filename });
const require = createRequire(filename);
moduleBody.call(
m.exports,
m.exports,
require,
m,
filename,
path.dirname(filename),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment