santifer/career-ops · error · TypeError

isMainModule expects import.meta.url (a file: URL), got a…

Error message

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.

What it means

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.

Solutions

  1. Change the call to pass import.meta.url.
  2. Convert a real path with `pathToFileURL(p).href` before calling.
  3. If you only wanted a path comparison, don't use this helper — compare `process.argv[1]` against the path yourself.

Example fix

// before
if (isMainModule(__filename)) { main(); }
// after
import { pathToFileURL } from 'node:url';
if (isMainModule(pathToFileURL(__filename).href)) { main(); }
Defensive patterns

Strategy: type-guard

Validate before calling

import { pathToFileURL } from 'node:url';
const 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);
if (isMainModule(asUrl(someValue))) cli();

Type guard

const looksLikeFileUrl = (v) => typeof v === 'string' && v.startsWith('file:');

Try / catch

try {
  if (isMainModule(entry)) cli();
} catch (err) {
  if (err instanceof TypeError && err.message.includes('filesystem path')) {
    cli(); // or: convert with pathToFileURL and retry
  } else throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: Windows development where drive paths masquerade as URLs; converting code from CJS where __filename was the norm; IDE snippets autocompleting filename instead of url.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of santifer/career-ops@aac998c7ed (2026-09-16). Data as JSON: /api/errors/e8e6a0c20c56ea0a. Report an issue: GitHub.

Appendix: source

Thrown at lib/is-main-module.mjs:100

  }

  if (!moduleUrl.startsWith('file:')) {
    // A Windows drive letter parses as a one-character URL scheme, so it has to
    // be excluded before the scheme test or `C:\co\pdf.mjs` reads as a URL.
    //
    // Matched WITHOUT requiring a following separator, because `C:repo\pdf.mjs`
    // is also a path — the drive-RELATIVE form, resolved against the current
    // directory on C:. Requiring `[\\/]` let that one through to the `return
    // false` below, which is the silent-suppression footgun this branch exists
    // to prevent. No registered URL scheme is a single letter, so treating
    // `X:` as a drive is unambiguous.
    const isPath = !/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(moduleUrl) || /^[a-zA-Z]:/.test(moduleUrl);
    if (isPath) {
      // LOUD, because the quiet alternative is the bug this module exists to
      // kill. `isMainModule(import.meta.filename)` would resolve, compare
      // false, skip the CLI tail, and exit 0 having printed nothing — #3170
      // reintroduced one argument at a time. A crash names the mistake instead.
      throw new TypeError(
        `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.',
      );
    }
    // A real non-file scheme (`data:`, `node:`, an http import). Not a
    // programmer error, and never the file named on the command line.
    return false;
  }

  // No argv[1] at all: `node -e`, `node --input-type=module`, a worker, the
  // REPL. Nothing was "run" in the sense the guard means. Checked AFTER the
  // argument validation so a bad call is caught wherever it happens.
  if (!process.argv[1]) return false;

  let modulePath;
  try {
    modulePath = fileURLToPath(moduleUrl);
  } catch {

View on GitHub (pinned to aac998c7ed)