🔥 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)
Astro Fundamentals••By Komal Nakrani

Fix: Invalid Glob Pattern in Astro

Astro's 'Invalid glob pattern' came from Astro.glob(), which was removed in Astro 6. Start patterns with ./ or /, or switch to import.meta.glob() or a loader.

#Astro#Errors#Glob#import.meta.glob

Astro’s “Invalid glob pattern” error means a glob pattern did not start with ./, ../ or /. It came from Astro.glob(), which Astro removed in version 6, so on Astro 7 you will only see it in an older project. The fix is the same either way: make the pattern a real relative or root path, and replace Astro.glob() with import.meta.glob() or a content collection.

Key Takeaways

  • Patterns must start with ./, ../ or /: './posts/*.md', not 'posts/*.md'.
  • Astro.glob() and this error were removed in Astro 6.
  • import.meta.glob() (from Vite) is the direct replacement for importing many files.
  • For Markdown and MDX content, a collection with the glob() loader is better.
  • The pattern must be a static string literal, not a variable.

The error

Invalid glob pattern.
Invalid glob pattern: `posts/*.md`. Glob patterns must start with './', '../' or '/'.
Hint: See https://docs.astro.build/en/guides/imports/#glob-patterns for more information on supported glob patterns.

Why it happens

Glob imports are resolved like normal imports, relative to the file that contains them. A bare pattern like posts/*.md looks like a package name, so it cannot be resolved. Older code did this:

---
// src/pages/blog/index.astro (Astro 5 and earlier)
const posts = await Astro.glob('posts/*.md');
---

On Astro 6 or 7, that code fails earlier, because Astro.glob no longer exists. Our Astro.glob removed fix covers that message.

Fix: import.meta.glob()

Use Vite’s import.meta.glob() with a relative pattern. Pass eager: true to get the modules directly:

---
// src/pages/blog/index.astro
const modules = import.meta.glob('../../content/posts/*.md', { eager: true });
const posts = Object.values(modules);
---
<ul>
  {posts.map((post) => <li><a href={post.url}>{post.frontmatter.title}</a></li>)}
</ul>

A pattern starting with / is resolved from the project root, so '/src/content/posts/*.md' also works. The pattern must be a literal string; Vite reads it at build time and cannot follow a variable.

Better fix: a content collection

For posts, docs or any Markdown you list and filter, define a collection instead. You get schema validation, typed data and IDs without manual globbing:

// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';

const posts = defineCollection({
  loader: glob({ pattern: '*.md', base: './src/content/posts' }),
  schema: z.object({ title: z.string() }),
});

export const collections = { posts };

Here pattern is relative to base, so no ./ prefix is needed. Then call getCollection('posts') in your page. The content collections docs and our content collections guide walk through it, and the Astro 6 to 7 upgrade tip lists other removed APIs. The official glob import docs cover the Vite function. If you would rather start on current APIs, every theme in the Astro themes catalog is an Astro 7 project.

FAQ

Why was Astro.glob removed?

Astro deprecated it in favor of Vite’s import.meta.glob() and content collections, which cover the same uses, and removed it in Astro 6.

Can I use a variable in import.meta.glob?

No. The pattern must be a string literal so Vite can resolve it at build time. Use several literal patterns if you need different folders.

Does the glob() loader need ./ in its pattern?

No. The loader’s pattern is relative to its base option. Only base is a path, such as './src/content/posts'.