isMainModule (lib/is-main-module.mjs)

career-ops · lib/is-main-module.mjs · javascript · MIT
files

* is-main-module.mjs — "was this file run, or imported?", answered correctly when the path used to reach it is a symlink (#3170). Every `.mjs` entrypoint here ends with a guard that only runs the CLI tail when the module IS the process entry. Around sixty of them hand-rolled that

// Source: https://github.com/career-ops-hq/career-ops | License: MIT | Copyright 2026 Santiago Fernández de Valderrama
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * is-main-module.mjs — "was this file run, or imported?", answered correctly
 * when the path used to reach it is a symlink (#3170).
 *
 * Every `.mjs` entrypoint here ends with a guard that only runs the CLI tail
 * when the module IS the process entry. Around sixty of them hand-rolled that
 * comparison, in at least six spellings, and all but one shared the same bug:
 *
 *   const isMain = process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;
 *
 * Node resolves the ESM entry through REALPATH before it sets `import.meta.url`;
 * `process.argv[1]` keeps whatever spelling the calle

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key