Skip to content
Latchkey

esbuild "import.meta is not available with the cjs output" in CI

Your code reads import.meta (usually import.meta.url), but esbuild is producing a CommonJS bundle where import.meta does not exist. The two module systems are incompatible here.

What this error means

The build warns or errors "import.meta is not available with the cjs output format and will be empty". At runtime import.meta.url is undefined, breaking path resolution.

node
> WARNING: "import.meta" is not available with the "cjs" output format
and will be empty

    src/paths.ts:1:18:
      1 | const here = import.meta.url;
        |                   ~~~~~~~~~~~

Common causes

CJS output with ESM-only syntax

import.meta is an ES module feature. A cjs output format cannot provide it, so it is stripped to empty.

Mixed module targets

Source written for ESM is being bundled to CJS for an older consumer, leaving import.meta dangling.

How to fix it

Emit ESM output

Switch the output format to esm so import.meta resolves normally.

build script
esbuild.build({ format: 'esm', platform: 'node', target: 'es2022' });

Use CJS-friendly path globals

If the output must be CJS, replace import.meta.url with __dirname/__filename or inject a define.

source
// CJS-safe
const here = __dirname;

How to prevent it

  • Keep the output format consistent with the syntax in source.
  • Use __dirname for CJS bundles and import.meta.url for ESM bundles.
  • Validate the built artifact in CI to catch empty import.meta early.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →