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

  1. Add `export const variables = { MY_VAR: 'string', OTHER: 'boolean' };` to src/env.js.
  2. Ensure `variables` is a plain object literal, not an enum/schema instance, string, or array.
  3. 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

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


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/9f7f9e13954f761e. Report an issue: GitHub.