{"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/aac998c7ed7248ea853b720ceeb1fdbeb322fc5d/lib/is-main-module.mjs#L63-L99","documentation":"isMainModule() determines whether the module it is called from is the entry point `node` was launched with, and requires its argument to be a non-empty string representing import.meta.url. This TypeError fires when the argument is not a string at all (undefined, null, a number) or is an empty string — i.e. the caller did not pass import.meta.url.","triggerScenarios":"Calling isMainModule() with no argument, with import.meta.filename, with __filename-like values that are undefined in ESM, or with an empty-string variable; also calling from a context without import.meta (CommonJS interop).","commonSituations":"Copy-pasting the guard into a file and forgetting the argument; refactor renamed the variable holding import.meta.url leaving it undefined; using the helper in a CJS file where import.meta does not exist.","solutions":["Pass import.meta.url exactly: `if (isMainModule(import.meta.url)) { ... }`.","If you have a filesystem path, convert it first: `pathToFileURL(__filename).href`.","In a CJS context, switch the file to ESM or use the CJS equivalent (`require.main === module`) instead."],"exampleFix":"// before\nif (isMainModule(import.meta.filename)) { cli(); }\n// after\nimport { pathToFileURL } from 'node:url';\nif (isMainModule(import.meta.url)) { cli(); }","handlingStrategy":"type-guard","validationCode":"if (typeof import.meta?.url !== 'string' || import.meta.url === '') {\n  throw new Error('not running as ESM; isMainModule needs import.meta.url');\n}\nif (isMainModule(import.meta.url)) cli();","typeGuard":"const canCheckMain = (meta) => typeof meta?.url === 'string' && meta.url !== '';","tryCatchPattern":"try {\n  if (isMainModule(import.meta.url)) cli();\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('isMainModule expects')) {\n    console.error('Pass import.meta.url, not filename/path.');\n  } else throw err;\n}","preventionTips":["Always call isMainModule(import.meta.url) — the literal expression, no intermediate variables.","Convert paths with pathToFileURL(p).href when you only have a path.","Use this helper only in ESM modules."],"tags":["esm","type-error","entrypoint","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"aac998c7ed7248ea853b720ceeb1fdbeb322fc5d","analyzedAt":"2026-09-16T06:35:29.214Z","contentChangedAt":"2026-09-16T06:35:29.214Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}