Astro throws “Glob patterns are not supported in the file loader” when the path you pass to file() contains a *. The file() loader reads a single JSON, YAML or TOML file that holds many entries. To load many files, one entry per file, use the glob() loader instead.
Key Takeaways
file()= one file, many entries.glob()= many files, one entry each.- Any
*in thefile()path triggers this error immediately.- Switching to
glob({ pattern, base })is usually a two-line change.- With
glob(), each entry’sidcomes from its file name.- Your schema stays the same either way.
The error
Glob patterns are not supported in the file loader.
Glob patterns are not supported in the `file` loader. Use the `glob` loader instead.
It appears during astro dev, astro sync or astro build, as soon as Astro sets up your collections from src/content.config.ts.
Why it happens
The two built-in loaders from astro/loaders solve different problems:
| Loader | Reads | Entries |
|---|---|---|
file('src/data/authors.json') |
One file | Each item in the array (or each key of the object) |
glob({ pattern: '*.json', base: './src/data/authors' }) |
Every matching file | One per file |
The file() loader checks its argument and rejects anything containing * before reading, because it has no way to merge several files into one list. A config like this fails:
// src/content.config.ts (fails)
import { defineCollection } from 'astro:content';
import { file } from 'astro/loaders';
const authors = defineCollection({
loader: file('src/data/authors/*.json'),
});
Fix: switch to glob()
If you have one file per entry, use glob() and keep your schema:
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const authors = defineCollection({
loader: glob({ pattern: '*.json', base: './src/data/authors' }),
schema: z.object({
name: z.string(),
role: z.string(),
}),
});
export const collections = { authors };
src/data/authors/komal.json becomes the entry with id komal. pattern is relative to base, so use **/*.json to include subfolders.
Fix: merge into one file
If the entries are small, keep file() and put them in a single file instead:
[
{ "id": "komal", "name": "Komal", "role": "Editor" },
{ "id": "sam", "name": "Sam", "role": "Writer" }
]
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { file } from 'astro/loaders';
import { z } from 'astro/zod';
const authors = defineCollection({
loader: file('src/data/authors.json'),
schema: z.object({ name: z.string(), role: z.string() }),
});
export const collections = { authors };
Each array item needs a unique id. One file is easier to edit for short lists like authors, tags or navigation; separate files suit entries that change independently or are edited by different people.
Check it
# Terminal
npx astro sync
astro sync regenerates collection types and reports loader errors without a full build. See the astro sync command tip for what it does, our content layer loaders guide for both loaders in depth, and the content collections docs. The official content loader reference lists the file() options. Themes in the Blog & Editorial category show both loaders in real projects.
FAQ
What is the difference between the file and glob loaders in Astro?
file() loads many entries from one data file. glob() loads one entry per file from every file that matches a pattern, including Markdown and MDX.
Can the Astro file loader read YAML?
Yes. It parses JSON, YAML and TOML by file extension, and accepts a custom parser option for other formats.
How do I load a folder of JSON files in Astro?
Use glob({ pattern: '*.json', base: './src/data/folder' }). Each JSON file becomes one entry, with its file name as the id.