sveltejs/kit · error · Error
${file} must export a variables object
Error message
${file} must export a variables object What it means
When a project has a src/env.js (or .ts) file, SvelteKit loads it during build and requires it to export a `variables` object mapping environment variable names to their types. This error is thrown when the module exports nothing, or exports a `variables` that is not an object (e.g. a string, array of entries, or missing entirely).
Source
Thrown at packages/kit/src/core/env.js:87
}
}
]
});
/** @type {Record<string, EnvVarConfig<any>>} */
let variables;
const runner = get_runner(vite, server);
/** @type {typeof import('../runtime/app/env/server.js')} */ (
await runner.import(`${runtime_directory}/app/env/server.js`)
).set_building();
try {
({ variables } = await runner.import(file));
if (!variables || typeof variables !== 'object') {
throw new Error(`${file} must export a variables object`);
}
// validate
for (const name of Object.keys(variables)) {
if (!valid_identifier.test(name) || reserved.has(name)) {
throw new Error(`Invalid environment variable name ${JSON.stringify(name)}`);
}
}
} catch (e) {
const error = /** @type {any} */ (e || {});
if (
error.code === 'ERR_MODULE_NOT_FOUND' &&
error.message?.includes(`Cannot find module '$app`)
) {
throw new Error(
`Cannot import \`$app/*\` modules other than \`$app/env\` inside \`src/env\``,
{ cause: e }View on GitHub (pinned to 03f1687fe6)
Solutions
- Add `export const variables = { MY_VAR: 'string', OTHER: 'boolean' };` to src/env.js.
- Ensure `variables` is a plain object literal, not an enum/schema instance, string, or array.
- Use the named export `variables` — a default export will not satisfy the check.
Example fix
// before (src/env.js)
export default { PUBLIC_KEY: 'string' };
// after
export const variables = { PUBLIC_KEY: 'string' }; Defensive patterns
Strategy: validation
Validate before calling
// run before build against src/env.js
const mod = await import('./src/env.js');
if (!mod.variables || typeof mod.variables !== 'object' || Array.isArray(mod.variables)) {
throw new Error('src/env.js must export a plain `variables` object');
} Type guard
function hasValidVariablesExport(mod) {
return mod !== null && typeof mod === 'object' &&
'variables' in mod && typeof mod.variables === 'object' && mod.variables !== null;
} Try / catch
try {
await viteBuild();
} catch (e) {
if (String(e.message).includes('must export a variables object')) {
console.error('Fix src/env.js: add `export const variables = { NAME: \'string\' }`');
} else throw e;
} Prevention
- Always use the named export `variables`, never default export, in src/env.js.
- Keep variables a plain object literal — avoid enums, Maps, or schema instances.
- Run a build after creating src/env.js to validate its shape early.
When it happens
Trigger: src/env.js exists but has no `export const variables = {...}`; `variables` is exported as something non-object (string, class instance like an enum, Map); the default export is used instead of a named `variables` export.
Common situations: Following old pre-1.0 SvelteKit env documentation; exporting `variables` as a zod schema or enum object (class instance) which fails the plain-object typeof check; typo like `variable` instead of `variables`.
Related errors
- ${keypath} cannot be empty
- Each member of ${keypath} must start with '.' — saw '${exten
- SvelteKit configuration (${keys}) no longer lives inside a `
- ${keypath} should be one of "${options}" or "${options[optio
- Cannot import `$app/*` modules other than `$app/env` inside
AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02).
Data as JSON: /api/errors/9f7f9e13954f761e.
Report an issue: GitHub.