Skip to content
Latchkey

Svelte "Cannot use 'import.meta' outside a module" in CI

import.meta is only valid inside an ES module. The error means a bundle that uses it (Vite emits import.meta.env) was executed as CommonJS, usually because the package is not flagged as a module or an old Node parsed it as a script.

What this error means

A build step or a server entry fails with "SyntaxError: Cannot use 'import.meta' outside a module", pointing at generated Vite output or a config file.

node
import.meta.env.MODE
        ^^^^
SyntaxError: Cannot use 'import.meta' outside a module
    at compileFunction (node:vm:...)

Common causes

The file is treated as CommonJS

A .js file in a package without "type": "module" is parsed as CommonJS, where import.meta is a syntax error.

An outdated Node on the runner

A very old Node version does not parse import.meta in the context the build runs it, so the syntax check fails.

How to fix it

Mark the package as an ES module

  1. Add "type": "module" to package.json, or rename the file to .mjs.
  2. Ensure config files (vite.config) use ESM syntax to match.
  3. Re-run the build.
package.json
{
  "type": "module"
}

Pin a current Node on the runner

Use a Node version that fully supports import.meta in modules.

.github/workflows/ci.yml
- uses: actions/setup-node@v4
  with:
    node-version: '20'

How to prevent it

  • Keep SvelteKit/Vite projects as ESM with "type": "module".
  • Match Node on the runner to the engines your tooling requires.
  • Write config files in ESM to avoid CommonJS parsing of import.meta.

Frequently asked questions

What causes ""Cannot use 'import.meta' outside a module""?
A .js file in a package without "type": "module" is parsed as CommonJS, where import.meta is a syntax error.
How do I fix "Cannot use 'import.meta' outside a module"?
Mark the package as an ES module

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card