Fix: Invalid Glob Pattern in Astro

Error message:
Invalid glob pattern.
Build & Compilation 2025-01-25

What Causes This Error?

This error occurs when you use Astro.glob() or import.meta.glob() with a pattern that doesn’t start with ./, ../, or /. Glob patterns must use relative or absolute paths.

The Problem

---
// ❌ Pattern doesn't start with ./ or ../
const posts = await Astro.glob('content/blog/*.md');

// ❌ Using bare path
const images = import.meta.glob('assets/*.jpg');
---

The Fix

Use Relative Path Prefix

---
// ✅ Start with ./
const posts = await Astro.glob('./content/blog/*.md');

// ✅ Start with ../
const images = import.meta.glob('../assets/*.jpg');
---

Common Scenarios

Content in Same Directory

---
// From src/pages/blog/index.astro

// ❌ Wrong
const posts = await Astro.glob('*.md');

// ✅ Correct
const posts = await Astro.glob('./*.md');
---

Parent Directory

---
// From src/pages/index.astro accessing src/content

// ❌ Wrong
const content = await Astro.glob('content/**/*.md');

// ✅ Correct
const content = await Astro.glob('../content/**/*.md');
---

Absolute Path from Project Root

---
// Using / for absolute path from project root

// ✅ Absolute from src/
const allPosts = import.meta.glob('/src/content/blog/*.md');
---

Multiple File Types

---
// ✅ Glob multiple extensions
const files = import.meta.glob('../assets/*.{jpg,png,webp}');
---

import.meta.glob with Options

---
// ✅ With eager loading
const images = import.meta.glob('../assets/*.jpg', { eager: true });

// ✅ With query
const imageUrls = import.meta.glob('../assets/*.jpg', {
  query: '?url',
  import: 'default',
});
---

Nested Directories

---
// ✅ Recursive glob with **
const allContent = await Astro.glob('../content/**/*.md');

// ✅ Specific depth
const topLevelOnly = await Astro.glob('../content/*.md');
---

Path Aliases Don’t Work with Glob

---
// ❌ Path aliases don't work with glob
const posts = await Astro.glob('@content/blog/*.md');

// ✅ Use relative paths instead
const posts = await Astro.glob('../content/blog/*.md');
---

Dynamic Glob Patterns

---
// ❌ Variables in glob patterns don't work
const folder = 'blog';
const posts = await Astro.glob(`./${folder}/*.md`);

// ✅ Use static patterns
const blogPosts = await Astro.glob('./blog/*.md');
const docsPosts = await Astro.glob('./docs/*.md');
---

Valid Pattern Examples

---
// All valid patterns
const a = await Astro.glob('./posts/*.md');           // Current dir
const b = await Astro.glob('../content/**/*.md');     // Parent dir
const c = await Astro.glob('../../data/*.json');      // Up two levels
const d = import.meta.glob('/src/assets/*.jpg');      // Absolute from src

// ❌ Invalid patterns
const e = await Astro.glob('posts/*.md');             // Missing ./
const f = await Astro.glob('content/**/*.md');        // Missing prefix
---

Escaping Special Characters

---
// If folder has special characters
const files = await Astro.glob('./my-folder/*.md');    // Hyphens OK
const files = await Astro.glob('./my_folder/*.md');    // Underscores OK

// Brackets need escaping in some contexts
const files = await Astro.glob('./posts/\\[slug\\]/*.md');
---

Quick Checklist

  • Patterns must start with ./, ../, or /
  • Use ./ for current directory
  • Use ../ for parent directories
  • Use /src/ for absolute paths
  • Path aliases don’t work with glob
  • Patterns must be static (no variables)