{"record":{"id":"e8e6a0c20c56ea0a","repo":"santifer/career-ops","slug":"ismainmodule-expects-import-meta-url-a-file-url","errorCode":null,"errorMessage":"isMainModule expects import.meta.url (a file: URL), got a filesystem path: ${moduleUrl}. Returning false here would silently suppress the CLI, which is the defect #3170 fixed.","messagePattern":"isMainModule expects import\\.meta\\.url \\(a file: URL\\), got a filesystem path: (.+?)\\. Returning false here would silently suppress the CLI, which is the defect #3170 fixed\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/is-main-module.mjs","lineNumber":100,"sourceCode":"  }\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.\n      throw new TypeError(\n        `isMainModule expects import.meta.url (a file: URL), got a filesystem path: ${moduleUrl}. ` +\n        'Returning false here would silently suppress the CLI, which is the defect #3170 fixed.',\n      );\n    }\n    // A real non-file scheme (`data:`, `node:`, an http import). Not a\n    // programmer error, and never the file named on the command line.\n    return false;\n  }\n\n  // No argv[1] at all: `node -e`, `node --input-type=module`, a worker, the\n  // REPL. Nothing was \"run\" in the sense the guard means. Checked AFTER the\n  // argument validation so a bad call is caught wherever it happens.\n  if (!process.argv[1]) return false;\n\n  let modulePath;\n  try {\n    modulePath = fileURLToPath(moduleUrl);\n  } catch {","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/santifer/career-ops/blob/aac998c7ed7248ea853b720ceeb1fdbeb322fc5d/lib/is-main-module.mjs#L82-L118","documentation":"isMainModule() deliberately throws (rather than returning false) when handed a filesystem path instead of a file: URL. Returning false would silently skip the CLI tail and exit 0 with no output — the exact bug #3170 fixed — so the module fails loudly. Windows drive-letter paths are detected as paths even though 'C:' looks like a URL scheme.","triggerScenarios":"Calling isMainModule(import.meta.filename), isMainModule(__filename), or any '/abs/path.mjs' or 'C:\\co\\pdf.mjs' string; also any string without a URL scheme or with a one-character (drive-letter) scheme.","commonSituations":"Windows development where drive paths masquerade as URLs; converting code from CJS where __filename was the norm; IDE snippets autocompleting filename instead of url.","solutions":["Change the call to pass import.meta.url.","Convert a real path with `pathToFileURL(p).href` before calling.","If you only wanted a path comparison, don't use this helper — compare `process.argv[1]` against the path yourself."],"exampleFix":"// before\nif (isMainModule(__filename)) { main(); }\n// after\nimport { pathToFileURL } from 'node:url';\nif (isMainModule(pathToFileURL(__filename).href)) { main(); }","handlingStrategy":"type-guard","validationCode":"import { pathToFileURL } from 'node:url';\nconst asUrl = (v) => (typeof v === 'string' && /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(v) && !/^[a-zA-Z]:$/.test(v.slice(0, 2)) ? v : pathToFileURL(String(v)).href);\nif (isMainModule(asUrl(someValue))) cli();","typeGuard":"const looksLikeFileUrl = (v) => typeof v === 'string' && v.startsWith('file:');","tryCatchPattern":"try {\n  if (isMainModule(entry)) cli();\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('filesystem path')) {\n    cli(); // or: convert with pathToFileURL and retry\n  } else throw err;\n}","preventionTips":["Use import.meta.url literally; never import.meta.filename or __filename.","On Windows, remember drive-letter strings are paths, not URLs — always pathToFileURL them.","Search codebases for isMainModule(import.meta.filename) as a lint habit."],"tags":["esm","type-error","entrypoint","windows","argument-validation"],"backgroundTag":"invalid-argument-format","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"}