withastro/astro · warning

The collection ${JSON.stringify(collection)} does not exist

Error message

The collection ${JSON.stringify(collection)} does not exist or is empty. Please check your content config file for errors.

What it means

getCollection(name) was called at runtime for a collection the data store does not contain; Astro logs this warning and returns an empty array. It fires when the name does not match a key in the content config (typo, rename) or when the collection's loader loaded zero entries or failed to run at all.

Source

Thrown at packages/astro/src/content/runtime.ts:136

			const result = [];
			for (const rawEntry of await store.values<DataEntry>(collection)) {
				const data = resolveEntryData(rawEntry, imageAssetMap);

				let entry = {
					...rawEntry,
					data,
					collection,
				};

				if (hasFilter && !filter(entry)) {
					continue;
				}
				result.push(entry);
			}
			return result;
		} else {
			logger.warn(
				'content',
				`The collection ${JSON.stringify(
					collection,
				)} does not exist or is empty. Please check your content config file for errors.`,
			);
			return [];
		}
	};
}

type ContentEntryResult = {
	id: string;
	slug: string;
	body: string;
	collection: string;
	data: Record<string, any>;
	digest?: string | number;
	render(): Promise<RenderResult>;

View on GitHub (pinned to e294953aa8)

Solutions

  1. Check the exact collection key in the defineCollection call in the content config and use it verbatim
  2. Scan dev-server/build logs for loader errors (missing directory, no matches, no entry type) that left the collection empty
  3. Run astro sync or restart the dev server so the store regenerates
  4. Guard rendering code against an empty array so pages degrade gracefully

Example fix

// before
const posts = await getCollection('blog'); // config defines 'posts'

// after
const posts = await getCollection('posts');
Defensive patterns

Strategy: type-guard

Validate before calling

// Fail at compile time on unknown collection names using generated types
import type { CollectionKey } from 'astro:content';
const VALID_COLLECTIONS = ['blog', 'authors', 'tags'] as const satisfies readonly CollectionKey[];
function assertCollection(name: string): asserts name is CollectionKey {
  if (!VALID_COLLECTIONS.includes(name as never)) {
    throw new Error(`Unknown collection "${name}" — check src/content.config.ts`);
  }
}

Type guard

import type { CollectionKey } from 'astro:content';
const KNOWN: readonly string[] = ['blog', 'authors', 'tags'];
function isKnownCollection(name: string): name is CollectionKey {
  return (KNOWN as readonly string[]).includes(name);
}

Prevention

When it happens

Trigger: getCollection('blog') when the config defines 'posts'; the content config file failing to load so no collections register; the collection's loader erroring (bad base dir, zero glob matches — each has its own warning) leaving the store empty for that name.

Common situations: Renaming collections without updating queries; collection names built dynamically from URL parameters; migrating from legacy src/content/config.ts to the Content Layer where definitions moved.

Related errors


AI-assisted analysis of withastro/astro@e294953aa8 (2026-09-09). Data as JSON: /api/errors/3a9107d5bd483f93. Report an issue: GitHub.