🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Fix: Content Entry Is Missing an ID in Astro

Fix "content entry is missing an id" in Astro: give every item in a file() data file an id, and return string ids from custom loaders, with before and after code.

#Astro#Errors#Content Collections#Loaders

“Content entry is missing an id” covers two related Astro messages. The file() loader logs “Item in src/data/team.json is missing an id or slug field.” and skips that item. A custom loader that returns an entry without a string id fails with “Content loader returned an entry with an invalid id.” and the hint “Make sure that the id of the entry is a string.” Give every entry a unique string id, and convert numeric ids with String().

Key Takeaways

  • Arrays loaded with file() need an id (or slug) on every item; items without one are skipped.
  • Or use an object keyed by id, and Astro uses the keys.
  • Custom loaders must return id as a string. API ids are often numbers; wrap them in String().
  • Ids must be unique within a collection, or later entries trigger a duplicate error.

Case 1: a data file without ids

[
  { "name": "Ana Ruiz", "role": "Design" },
  { "name": "Sam Lee", "role": "Engineering" }
]

With file('src/data/team.json'), each item gets skipped with “Item in src/data/team.json is missing an id or slug field.” in the terminal. The collection ends up empty, so the page usually renders nothing rather than crashing, which makes this easy to miss. Add an id to each item:

[
  { "id": "ana-ruiz", "name": "Ana Ruiz", "role": "Design" },
  { "id": "sam-lee", "name": "Sam Lee", "role": "Engineering" }
]

Or switch to an object where each key is the id:

{
  "ana-ruiz": { "name": "Ana Ruiz", "role": "Design" },
  "sam-lee": { "name": "Sam Lee", "role": "Engineering" }
}

The same rules apply to YAML and TOML files. Numeric ids such as "id": 7 are accepted by file() and converted to the string "7".

Case 2: a custom loader returns bad ids

An inline loader returns an array of objects, and each needs a string id:

// src/content.config.ts (before)
const products = defineCollection({
  loader: async () => {
    const res = await fetch('https://api.example.com/products');
    return res.json(); // items look like { sku: 1042, name: '...' }
  },
  schema: z.object({ name: z.string() }),
});

Here the API uses sku, so there’s no id at all. If it used a numeric id, validation would fail with “Content entry id must be a string” inside the invalid id error. Map the response:

// src/content.config.ts (after)
import { defineCollection } from 'astro:content';
import { z } from 'astro/zod';

const products = defineCollection({
  loader: async () => {
    const res = await fetch('https://api.example.com/products');
    const items: Array<{ sku: number; name: string }> = await res.json();
    return items.map((item) => ({ id: String(item.sku), name: item.name }));
  },
  schema: z.object({ name: z.string() }),
});

export const collections = { products };

The error message prints the offending entry as JSON, so you can see which field to use as the id.

Case 3: object loaders calling store.set()

A loader object with a load() function writes entries itself. Pass a string id there too:

store.set({ id: String(item.sku), data: await parseData({ id: String(item.sku), data: item }) });

Keep ids unique

Two items with the same id in a file() collection produce a duplicate warning or error, depending on your config. Use stable values like slugs or database keys, not array positions, so ids don’t change when you reorder the file. The duplicate entry slug fix covers that case.

The content layer loaders guide explains file(), glob() and custom loaders, and our content docs show the data collections in Komal Astro themes. See the official file() loader docs for parser options.

FAQ

Does the file() loader accept slug instead of id?

Yes. It uses id if present, otherwise slug. Items with neither are skipped with an error in the log.

Why is my data collection empty without an error page?

The file() loader logs missing ids and skips those items instead of throwing. Check the terminal for “is missing an id or slug field”.

Can an Astro content entry id be a number?

Not from a custom loader; ids must be strings, so convert with String(). The file() loader converts numeric ids for you.