Files
scriptc/tests/fixtures/npm/cases/create-require/main.ts
Chris Tate 4abd8e4fff node:module compiles: createRequire's static erasure and the baked builtinModules
- createRequire(import.meta.url) (or __filename / import.meta.filename) in a const binding is compile-time plumbing that emits nothing; each require call through it resolves per site against the binding's own file, and the inline createRequire(import.meta.url)("spec") spelling erases identically
- The erasure per target: a builtin spec makes the binding a namespace import in const clothing (namespace, destructured, and inline-member spellings all key the existing module tables, with the fallback-typed 'as typeof import(...)' cast idiom stripped); a relative .json document validates at compile time and lowers to json.parse over its baked text (JSON.parse's checked-dynamic unknown stance — exactly Node's value); a bare name nothing installed resolves compiles to Node's catchable MODULE_NOT_FOUND throw (the optional-dependency try/require pattern); an installed npm package embeds under --dynamic through the island's NEW require-condition entry (NpmGraphBuilder.addRequire — a dual package answers its CJS arm, exactly Node's createRequire) and reports the requires-dynamic diagnostic in a static build
- Dynamic specifiers, '#' project imports, relative non-.json targets, --npm-static opt-ins, and non-this-file bases all fence by name; the island-handle rescue skips the plumbing decls so --dynamic builds erase them too
- module.builtinModules bakes as Node v24's exact list (a fresh string[] per read, pinned to the compat target so emitted programs stay host-independent); isBuiltin and syncBuiltinESMExports fence per site with pointed hints, and the createRequire hint, its diagnostics snapshot, the surface manifest, and the npm-static offender wording follow
- Corpus 2606 pins json/builtin/builtinModules/MODULE_NOT_FOUND differentially on both backends and under ASan; the npm create-require fixture case pins cjszoo's module.exports, dual's require-condition CJS arm, and the missing-package rejection against Node
2026-07-24 10:32:20 -05:00

30 lines
1.2 KiB
TypeScript

// @dynamic
// createRequire over INSTALLED packages: the require resolves under
// Node's "require" condition at build time — a CJS package's value IS
// module.exports (the lexed facade's default), a dual package answers
// its CJS arm (where the import condition would load the ESM arm), and
// a bare name nothing installed resolves throws Node's catchable
// MODULE_NOT_FOUND at the call.
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
// A plain CJS package: module.exports members through the island.
const zoo = require("cjszoo") as any;
console.log("zoo:", zoo.alpha as string, zoo.extra as string, zoo.gamma() as string);
// The dual package: "exports" splits import/require — require() loads
// the CJS arm, exactly Node's createRequire.
const dual = require("dual") as any;
console.log("dual:", dual.flavor as string);
// Missing package: catchable MODULE_NOT_FOUND (the optional-dependency
// try/require pattern).
try {
require("definitely-not-installed-here");
console.log("SHOULD NOT PRINT");
} catch (e) {
const err = e as NodeJS.ErrnoException;
console.log("missing:", err.code, `${err.message}`.split("\n")[0]);
}