{"record":{"id":"7f8b07fbb2e6f52e","repo":"santifer/career-ops","slug":"ismainmodule-expects-import-meta-url-got-typeof","errorCode":null,"errorMessage":"isMainModule expects import.meta.url; got ${typeof moduleUrl === 'string' ? 'an empty string' : typeof moduleUrl}","messagePattern":"isMainModule expects import\\.meta\\.url; got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/is-main-module.mjs","lineNumber":81,"sourceCode":" */\nconst canonicalize = realpathSync.native ?? realpathSync;\n\n/**\n * True when the module identified by `moduleUrl` is the process entrypoint.\n *\n * Call it as `isMainModule(import.meta.url)`. Returns false when the module was\n * imported rather than run, which is what keeps a CLI tail from firing inside\n * `node --test`, `test-all.mjs`, or any script that imports the module's\n * exported functions.\n *\n * @param {string} moduleUrl - The caller's `import.meta.url`. A `file:` URL, and\n *   deliberately nothing else — see the throw below.\n * @returns {boolean} True when this module is what `node` was pointed at.\n * @throws {TypeError} When handed a filesystem path instead of a `file:` URL.\n */\nexport function isMainModule(moduleUrl) {\n  if (typeof moduleUrl !== 'string' || moduleUrl === '') {\n    throw new TypeError(`isMainModule expects import.meta.url; got ${typeof moduleUrl === 'string' ? 'an empty string' : typeof moduleUrl}`);\n  }\n\n  if (!moduleUrl.startsWith('file:')) {\n    // A Windows drive letter parses as a one-character URL scheme, so it has to\n    // be excluded before the scheme test or `C:\\co\\pdf.mjs` reads as a URL.\n    //\n    // Matched WITHOUT requiring a following separator, because `C:repo\\pdf.mjs`\n    // is also a path — the drive-RELATIVE form, resolved against the current\n    // directory on C:. Requiring `[\\\\/]` let that one through to the `return\n    // false` below, which is the silent-suppression footgun this branch exists\n    // to prevent. No registered URL scheme is a single letter, so treating\n    // `X:` as a drive is unambiguous.\n    const isPath = !/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(moduleUrl) || /^[a-zA-Z]:/.test(moduleUrl);\n    if (isPath) {\n      // LOUD, because the quiet alternative is the bug this module exists to\n      // kill. `isMainModule(import.meta.filename)` would resolve, compare\n      // false, skip the CLI tail, and exit 0 having printed nothing — #3170\n      // reintroduced one argument at a time. A crash names the mistake instead.","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/santifer/career-ops/blob/1696bec4d021768e7359f9aad6b329cba883da20/lib/is-main-module.mjs#L63-L99","documentation":"isMainModule() answers 'was this file the entry point node ran?' and it can only answer that from the module's `import.meta.url`, a `file:` URL string. The function rejects non-string or empty-string inputs with a TypeError at the very first check, before any comparison, because such a value cannot identify a module. This guards against callers passing undefined, null, or accidentally shadowed variables.","triggerScenarios":"Calling isMainModule(undefined), isMainModule(null), isMainModule(someObject), or isMainModule('') — any argument that is not a non-empty string.","commonSituations":"Copy-pasting the call into CommonJS code where `import.meta` doesn't exist (so the variable is undefined); passing import.meta.filename or __filename instead of import.meta.url; a bundler/transpiler that rewrites import.meta.url to '' or drops it.","solutions":["Pass `import.meta.url` directly: `if (isMainModule(import.meta.url)) {...}`","If in a CommonJS file, don't use this helper — use `require.main === module` instead","If a build tool empties import.meta.url, configure it to preserve ESM metadata (e.g. emit ESM output)"],"exampleFix":"// before\nif (isMainModule(import.meta.filename)) { cli(); }\n// after\nif (isMainModule(import.meta.url)) { cli(); }","handlingStrategy":"type-guard","validationCode":"if (typeof import.meta.url === 'string' && import.meta.url !== '') { isMain(import.meta.url); }","typeGuard":"const isModuleUrl = (v) => typeof v === 'string' && v.startsWith('file:');","tryCatchPattern":"try { if (isMainModule(import.meta.url)) cli(); } catch (e) { if (e instanceof TypeError) console.error('Bad moduleUrl passed to isMainModule:', e.message); else throw e; }","preventionTips":["Always call it as isMainModule(import.meta.url), never with a variable holding filename/path","Don't post-process import.meta.url (resolve, decode) before passing it","In CommonJS files use require.main === module instead of this helper"],"tags":["esm","typeerror","import-meta"],"backgroundTag":"invalid-argument-value","analyzedSha":"1696bec4d021768e7359f9aad6b329cba883da20","analyzedAt":"2026-09-01T19:19:23.111Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}