sveltejs/kit · error · Error

You must specify the `main` key in ${config_path} if you wan

Error message

You must specify the `main` key in ${config_path} if you want to deploy a Worker alongside your static assets. Otherwise, remove the `assets.binding` key if you only want to deploy static assets.

What it means

This is the inverse of error 35: an `assets.binding` exists, which declares 'a Worker will serve/interact with assets', but no `main` key names the Worker script. The adapter cannot tell whether you forgot the `main` entry or meant a static-only deployment, so it throws with both remediation options.

Source

Thrown at packages/adapter-cloudflare/utils.js:49

	// we need the `assets.directory` key so that the static assets are deployed
	if ((wrangler_config.main || wrangler_config.assets) && !wrangler_config.assets?.directory) {
		throw new Error(
			`You must specify the \`assets.directory\` key in ${config_path}. Consult https://developers.cloudflare.com/workers/static-assets/binding/#directory`
		);
	}

	// we need the `assets.binding` key so that the Worker can access the static assets
	if (wrangler_config.main && !wrangler_config.assets?.binding) {
		throw new Error(
			`You must specify the \`assets.binding\` key in ${config_path} before deploying your Worker. Consult https://developers.cloudflare.com/workers/static-assets/binding/#binding`
		);
	}

	// the user might have forgot the `main` key or should remove the `assets.binding`
	// key to deploy static assets without a Worker
	if (!wrangler_config.main && wrangler_config.assets?.binding) {
		throw new Error(
			`You must specify the \`main\` key in ${config_path} if you want to deploy a Worker alongside your static assets. Otherwise, remove the \`assets.binding\` key if you only want to deploy static assets.`
		);
	}
}

/**
 * Extracts the redirect source from each line of a [_redirects](https://developers.cloudflare.com/pages/configuration/redirects/)
 * file so we can exclude them in [_routes.json](https://developers.cloudflare.com/pages/functions/routing/#create-a-_routesjson-file)
 * to ensure the redirect is invoked instead of the Cloudflare Worker.
 * @param {string} file_contents
 * @returns {string[]}
 */
export function parse_redirects(file_contents) {
	/** @type {string[]} */
	const redirects = [];

	for (const line of file_contents.split('\n')) {
		const content = line.trim();

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Add `"main": "<worker entry point>"` to wrangler.jsonc if you want a Worker alongside static assets (the adapter normally sets this to the built server).
  2. Remove `assets.binding` if you only want to serve static assets with no Worker.
  3. Re-run `vite build` and let the adapter rewrite the config before hand-editing further.

Example fix

// before
{
  "assets": { "directory": ".svelte-kit/cloudflare", "binding": "ASSETS" }
}

// after (Worker deploy)
{
  "main": "build/server/index.js",
  "assets": { "directory": ".svelte-kit/cloudflare", "binding": "ASSETS" }
}
Defensive patterns

Strategy: validation

Validate before calling

if (!cfg.main && cfg.assets?.binding) {
  throw new Error('assets.binding requires a main worker entry, or remove the binding');
}

Type guard

function isStaticOnlyConfig(config) {
  return !config?.main && !config?.assets?.binding;
}

Try / catch

try {
  await build();
} catch (e) {
  if (String(e).includes('`main` key')) {
    console.error('Add `main` or remove assets.binding');
    process.exit(1);
  }
  throw e;
}

Prevention

When it happens

Trigger: wrangler config defines `assets.binding` (and possibly `assets.directory`) but has no top-level `main` key, encountered during the adapter's config validation.

Common situations: Deleting the `main` field after restructuring the build; copy-pasting an assets config from a static-assets example into an adapter project; renaming the worker entry and removing `main` accidentally.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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