sveltejs/kit · error

Invalid alias key: ${key}

Error message

Invalid alias key: ${key}

What it means

When generating tsconfig path mappings from the SvelteKit `kit.alias` config, each key must look like a valid TS paths pattern, optionally ending in `/*` (alias_key = /^(.+?)(\/\*)?$/). A key such as an empty string or a malformed wildcard fails this regex and sync throws during write_tsconfig.

Source

Thrown at packages/kit/src/core/sync/write_tsconfig/index.js:253

/**
 * Generates tsconfig path aliases from kit's aliases and the package.json `imports` field.
 * Related to vite alias creation.
 *
 * @param {import('types').ValidatedConfig} config
 * @param {string} root
 * @returns {Record<string, string[]>}
 */
function get_paths(config, root) {
	const alias = {
		...config.alias
	};

	/** @type {Record<string, string[]>} */
	const paths = {};

	for (const [key, value] of Object.entries(alias)) {
		const key_match = alias_key.exec(key);
		if (!key_match) throw new Error(`Invalid alias key: ${key}`);

		const value_match = alias_value.exec(value);
		if (!value_match) throw new Error(`Invalid alias value: ${value}`);

		const resolved = path.resolve(root, remove_trailing_slashstar(value));
		const slashstar = key_match[2];

		if (slashstar) {
			paths[key] = [resolved + '/*'];
		} else {
			paths[key] = [resolved];
			const fileending = value_match[4];

			if (!fileending && !(key + '/*' in alias)) {
				paths[key + '/*'] = [resolved + '/*'];
			}
		}
	}

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Fix the alias key in svelte.config.js to match the TS paths form, e.g. '$lib': 'src/lib' or '$lib/*': 'src/lib/*'.
  2. Remove empty or otherwise invalid keys from kit.alias.
  3. Use the documented $lib alias instead of custom keys when possible, since it is provided automatically.

Example fix

// before (svelte.config.js)
alias: { '': 'src/lib', 'utils/': 'src/utils' }
// after
alias: { '$lib': 'src/lib', 'utils': 'src/utils', 'utils/*': 'src/utils/*' }
Defensive patterns

Strategy: validation

Validate before calling

const ALIAS_KEY = /^(.+?)(\/\*)?$/;
for (const key of Object.keys(config.kit?.alias ?? {})) {
  if (!key || !ALIAS_KEY.test(key)) throw new Error(`Invalid kit.alias key: ${JSON.stringify(key)}`);
}

Type guard

function isValidAliasKey(key) {
  return typeof key === 'string' && key.length > 0 && /^(.+?)(\/\*)?$/.test(key);
}

Try / catch

try {
  await runSync();
} catch (e) {
  if (e.message.startsWith('Invalid alias key')) {
    console.error('Fix kit.alias in svelte.config.js:', e.message);
  }
  throw e;
}

Prevention

When it happens

Trigger: kit.alias entries with invalid keys, e.g. alias: { '': 'src/lib' }, alias: { '*/': 'src' }, or keys containing characters that break the pattern while expecting wildcard semantics in the middle.

Common situations: Migrating from webpack-style alias objects where empty or odd keys were tolerated; typos like trailing slashes without the /*; programmatic config generation producing empty keys.

Related errors


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