Skip to content
Latchkey

Astro "Cannot use import statement outside a module" in CI

Node tried to execute a file containing ESM import statements as CommonJS. Astro config and integrations are ESM, so a mismatched module type or extension makes Node refuse to parse them.

What this error means

The build crashes early with "SyntaxError: Cannot use import statement outside a module" pointing at astro.config.mjs, an integration, or a dependency loaded by the build.

astro
import { defineConfig } from 'astro/config';
^^^^^^
SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm)

Common causes

A .js config without ESM enabled

The config is astro.config.js while package.json lacks "type": "module", so Node parses it as CommonJS and rejects import.

A CommonJS dependency loaded as ESM or vice versa

A plugin published only as CommonJS is imported in an ESM context, or a transform step strips the module flag.

How to fix it

Use the .mjs config or set type module

Rename the config to astro.config.mjs, or declare the package as ESM so .js is parsed as a module.

package.json
{
  "type": "module"
}

Match the integration to the module system

Import CommonJS-only integrations with a default import, or upgrade to an ESM-published version.

astro.config.mjs
// CommonJS plugin
import pkg from 'some-cjs-plugin';
const { plugin } = pkg;

How to prevent it

  • Use astro.config.mjs so the config is always ESM.
  • Set "type": "module" when using .js for ESM files.
  • Check whether new integrations ship ESM before importing them.

Related guides

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