What Causes This Error?
This error occurs when your glob pattern doesn’t match any files. This usually happens due to typos in the path, wrong file extensions, or missing files.
The Problem
---
// ❌ No files match this pattern
const posts = await Astro.glob('./posts/*.md'); // Directory is empty or wrong path
---
The Fix
Verify Files Exist
# Check if files exist at the path
ls -la src/posts/
# Verify file extensions
ls src/posts/*.md
Correct the Path
---
// ✅ Correct path to existing files
const posts = await Astro.glob('../content/blog/*.md');
---
Common Scenarios
Wrong Directory
---
// From src/pages/index.astro
// ❌ Wrong path
const posts = await Astro.glob('./blog/*.md');
// ✅ Correct relative path
const posts = await Astro.glob('../content/blog/*.md');
---
Wrong File Extension
---
// ❌ Looking for .md but files are .mdx
const posts = await Astro.glob('./posts/*.md');
// ✅ Match the actual extension
const posts = await Astro.glob('./posts/*.mdx');
// ✅ Or match both
const posts = await Astro.glob('./posts/*.{md,mdx}');
---
Case Sensitivity
---
// ❌ Wrong case (files are .MD on some systems)
const posts = await Astro.glob('./posts/*.md');
// Check actual file extensions in terminal:
// ls -la src/posts/
---
Empty Directory
---
// Handle empty results gracefully
const posts = await Astro.glob('./posts/*.md');
// Check if any posts exist
if (posts.length === 0) {
console.warn('No posts found');
}
---
{posts.length > 0 ? (
posts.map(post => <Article post={post} />)
) : (
<p>No posts available</p>
)}
Nested Directories
---
// ❌ Only matches top level
const posts = await Astro.glob('./content/*.md');
// ✅ Match nested directories with **
const posts = await Astro.glob('./content/**/*.md');
---
Debug with Console
---
// Debug the pattern
import { glob } from 'astro/loaders';
// Log the resolved path
console.log('Looking for posts in:', import.meta.url);
try {
const posts = await Astro.glob('../content/blog/*.md');
console.log('Found posts:', posts.length);
} catch (e) {
console.error('Glob error:', e);
}
---
Check File Structure
Expected structure:
src/
├── content/
│ └── blog/
│ ├── post-1.md ← Files need to exist here
│ ├── post-2.md
│ └── post-3.md
└── pages/
└── index.astro ← Globbing from here
---
// From src/pages/index.astro
const posts = await Astro.glob('../content/blog/*.md');
---
Using Content Collections Instead
// src/content/config.ts
import { defineCollection, z } from 'astro:content';
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
}),
});
export const collections = { blog };
---
// src/pages/index.astro
import { getCollection } from 'astro:content';
// ✅ Content collections provide better errors
const posts = await getCollection('blog');
---
Verify with import.meta.glob
---
// Debug by checking what import.meta.glob finds
const modules = import.meta.glob('../content/blog/*.md');
console.log('Found modules:', Object.keys(modules));
// If empty, the path is wrong
// If populated, use:
const posts = await Astro.glob('../content/blog/*.md');
---
Quick Checklist
- Verify files exist at the specified path
- Check file extension matches pattern
- Use
**for nested directories - Check case sensitivity
- Use relative path from current file
- Consider Content Collections for better DX