withastro/astro · error · Error

Glob patterns cannot start with `/`. Set the `base` option t

Error message

Glob patterns cannot start with `/`. Set the `base` option to a parent directory or use a relative path instead.

What it means

Thrown as a plain Error (no AstroError code) by the glob() loader constructor when any pattern starts with '/'. Absolute-style patterns are disallowed because they are interpreted relative to the filesystem root, not the project; the API requires either a relative pattern or an explicit base.

Source

Thrown at packages/astro/src/content/loaders/glob.ts:91

	}
	return pattern.startsWith(prefix);
}

export const secretLegacyFlag = Symbol('astro.legacy-glob');

/**
 * Loads multiple entries, using a glob pattern to match files.
 * @param pattern A glob pattern to match files, relative to the content directory.
 */

export function glob(globOptions: GlobOptions & { [secretLegacyFlag]?: boolean }): Loader {
	if (checkPrefix(globOptions.pattern, '../')) {
		throw new Error(
			'Glob patterns cannot start with `../`. Set the `base` option to a parent directory instead.',
		);
	}
	if (checkPrefix(globOptions.pattern, '/')) {
		throw new Error(
			'Glob patterns cannot start with `/`. Set the `base` option to a parent directory or use a relative path instead.',
		);
	}

	const isLegacy = !!globOptions[secretLegacyFlag];
	const userGenerateId =
		globOptions?.generateId ?? ((opts: GenerateIdOptions) => generateIdDefault(opts, isLegacy));
	// Coerce to string so numeric ids from YAML don't cause Set strict-equality mismatches
	// against string store keys in the untouched-entries cleanup. See #17624.
	const generateId = (opts: GenerateIdOptions) => String(userGenerateId(opts));

	const fileToIdMap = new Map<string, string>();

	return {
		name: 'glob-loader',
		load: async ({
			config,
			collection,

View on GitHub (pinned to d081033d5f)

Solutions

  1. Drop the leading slash to make the pattern relative: glob({ pattern: 'content/*.md' }).
  2. If you need a specific root, set base to that directory and keep the pattern relative.
  3. If you genuinely need an absolute location, pass an absolute file URL via base and a relative pattern.

Example fix

// before
glob({ pattern: '/content/posts/*.md' })
// after
glob({ pattern: 'content/posts/*.md' })
Defensive patterns

Strategy: validation

Validate before calling

function normalizeGlobPattern(pattern: string | string[]) {
  const arr = Array.isArray(pattern) ? pattern : [pattern];
  for (const p of arr) {
    if (p.startsWith('/')) throw new Error('Pattern must be relative; drop leading slash');
  }
}

Type guard

const isAbsolutePattern = (p: string) => p.startsWith('/');

Prevention

When it happens

Trigger: Calling glob({ pattern: '/content/*.md' }) or glob({ pattern: ['/posts/*.md'] }) — any element beginning with '/'.

Common situations: Mistakingly treating the pattern as project-root-relative (common coming from Next.js or other frameworks); copying a Unix path into the pattern; auto-generated patterns that prefix '/'.

Related errors


AI-assisted analysis of withastro/astro@d081033d5f (2026-08-12). Data as JSON: /api/errors/1cb4d1d85718a772. Report an issue: GitHub.