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.
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
- Add
"type": "module"to package.json, or rename the file to.mjs. - Ensure config files (vite.config) use ESM syntax to match.
- Re-run the build.
{
"type": "module"
}Pin a current Node on the runner
Use a Node version that fully supports import.meta in modules.
- 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.