What Causes This Error?
This error occurs when astro sync fails to generate TypeScript types for your content collections. Common causes include invalid config, schema errors, or file permission issues.
The Problem
$ npm run astro sync
# or
$ npx astro sync
Error: Failed to generate content types
The Fix
Check Config Syntax
// src/content/config.ts
import { defineCollection, z } from 'astro:content';
// ✅ Valid config
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
date: z.date(),
}),
});
export const collections = { blog };
Run Sync Manually
# Generate types
npx astro sync
# Or with verbose output
npx astro sync --verbose
Common Scenarios
Invalid Schema
// ❌ Invalid schema syntax
const blog = defineCollection({
schema: {
title: 'string', // Wrong - not Zod
},
});
// ✅ Valid Zod schema
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
}),
});
Missing Export
// ❌ Missing collections export
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
}),
});
// ✅ Export collections
export const collections = { blog };
Wrong File Location
# ❌ Wrong location
src/config.ts
content/config.ts
# ✅ Correct location
src/content/config.ts
TypeScript Errors in Config
// ❌ TypeScript error
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
// Missing import for reference()
author: reference('authors'),
}),
});
// ✅ Import reference
import { defineCollection, z, reference } from 'astro:content';
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
author: reference('authors'),
}),
});
Clear Generated Files
# Remove generated types and regenerate
rm -rf .astro
npx astro sync
Check for Circular References
// ❌ Circular reference
const posts = defineCollection({
schema: z.object({
relatedPost: reference('posts'), // References itself
}),
});
// ✅ Usually okay, but complex circulars can fail
// Simplify if needed
Node/npm Issues
# Clear node_modules and reinstall
rm -rf node_modules
npm install
# Then sync
npx astro sync
Permission Issues
# Check .astro directory permissions
ls -la .astro
# Fix permissions if needed
chmod -R 755 .astro
# Or remove and let it regenerate
rm -rf .astro
Config Validation
// src/content/config.ts
import { defineCollection, z } from 'astro:content';
// Log to debug
console.log('Loading content config...');
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
}),
});
console.log('Blog collection defined');
export const collections = { blog };
console.log('Collections exported');
Check Content Files
# Ensure content files exist
ls -la src/content/
# Check for invalid files
find src/content -name "*.md" -exec head -5 {} \;
IDE Type Generation
// tsconfig.json
{
"extends": "astro/tsconfigs/strict",
"compilerOptions": {
"types": ["astro/client"]
}
}
After Fixing
# Regenerate types
npx astro sync
# Restart dev server
npm run dev
# Or rebuild
npm run build
Quick Checklist
- Config file at
src/content/config.ts - Valid Zod schema syntax
-
collectionsobject exported - Run
npx astro syncafter changes - Clear
.astrofolder if issues persist - Check for TypeScript errors in config