import.meta.glob in Astro is Vite’s way to import many files with one pattern: import.meta.glob('../assets/gallery/*.jpg', { eager: true }) returns an object keyed by file path, with each module as the value. It replaced Astro.glob(), which was removed, and it is the right tool for images, components and raw files. For Markdown content you query and filter, use content collections instead.
Key Takeaways
- The pattern must be a string literal, resolved at build time, relative to the file or starting with
/for the project root.- Without
eager, values are functions that return a promise; witheager: true, values are the modules themselves.import: 'default'picks one export;query: '?raw'or'?url'changes what you get.- Use content collections for posts and pages that need schemas, sorting and
getCollection().
Eager and lazy
---
// src/pages/gallery.astro
import { Image } from 'astro:assets';
import type { ImageMetadata } from 'astro';
const images = import.meta.glob<{ default: ImageMetadata }>(
'/src/assets/gallery/*.{jpg,jpeg,png,webp}',
{ eager: true },
);
const photos = Object.entries(images)
.sort(([a], [b]) => a.localeCompare(b))
.map(([path, mod]) => ({ path, src: mod.default }));
---
<div class="grid">
{photos.map(({ path, src }) => (
<Image src={src} alt={path.split('/').pop()!.replace(/\.\w+$/, '').replaceAll('-', ' ')} width={600} />
))}
</div>
Every file matched becomes an optimized image, and adding a photo to the folder adds it to the page on the next build. Write real alt text for anything meaningful; deriving it from file names only works if the names describe the photo.
Without eager, each value is a loader:
// src/lib/icons.ts
const icons = import.meta.glob('/src/icons/*.svg', { query: '?raw', import: 'default' });
export async function getIcon(name: string): Promise<string | undefined> {
const load = icons[`/src/icons/${name}.svg`];
return load ? ((await load()) as string) : undefined;
}
Lazy globs only load the files you call, which keeps unused modules out of a page’s work.
Useful options
| Option | Effect | Example |
|---|---|---|
eager: true |
Import immediately, values are modules | Small sets you always use |
import: 'default' |
Return one named export instead of the module | Components, images |
query: '?raw' |
File contents as a string | SVG markup, code samples |
query: '?url' |
Final URL of the asset | Download links, preloads |
| Negative pattern | Exclude files | ['./*.astro', '!./_*.astro'] |
Load every component in a folder and render them by name:
---
// src/pages/blocks.astro
const blocks = import.meta.glob('../components/blocks/*.astro', { eager: true, import: 'default' });
const Hero = blocks['../components/blocks/Hero.astro'] as any;
---
<Hero />
Limits to know
- The pattern cannot use variables. A template string such as
./${dir}/*.mdfails because Vite resolves globs when it bundles. Glob a wider pattern and filter the keys. - Keys are paths exactly as the pattern wrote them, relative or root-based. Build lookups the same way.
- Globbing
.mdfiles gives youfrontmatterandContent, but no schema validation and norender(). That is the main reason to move content to collections; see our content collections guide.
If you are migrating, our Astro.glob removed fix shows the direct replacement. The Vite glob import docs list every option, and our Astro themes use globs for icon sets and galleries, as covered in the content docs.
FAQ
What replaced Astro.glob?
import.meta.glob with { eager: true } is the direct replacement. For Markdown and MDX content, content collections with getCollection() are the recommended approach.
Can I use a variable in import.meta.glob?
No. Vite resolves the pattern at build time, so it must be a string literal. Use a broader pattern and filter the returned keys at runtime.
How do I type import.meta.glob results in Astro?
Pass a type argument: import.meta.glob<{ default: ImageMetadata }>(...). With import: 'default', type the value directly, for example import.meta.glob<string>(..., { query: '?raw', import: 'default' }).