What Are Cursor Rules?
Cursor rules are project-specific instructions that tell Cursor's AI exactly how to generate code for your codebase. Think of them as a persistent system prompt that shapes every AI interaction.
Without rules, Cursor generates generic code. With well-crafted rules, it generates code that matches your team's conventions, uses your preferred libraries, and follows your architecture patterns.
Where to put them: - `.cursorrules` file in your project root (legacy, still supported) - `.cursor/rules/` directory for multiple rule files (recommended) - Global rules in Cursor Settings → Rules for AI
What rules can control: - Coding style and conventions - Preferred libraries and frameworks - File and folder structure - Error handling patterns - Testing approaches - What to avoid (anti-patterns, deprecated APIs)
Writing Effective Cursor Rules
Template for a Next.js project:
``` You are working on a Next.js 14+ App Router project with TypeScript.
Tech Stack: - Next.js with App Router (not Pages Router) - TypeScript (strict mode) - Tailwind CSS for styling - Prisma for database - NextAuth for authentication
Conventions: - Use server components by default, client components only when needed - Use 'use server' for server actions - Name files with kebab-case - Use named exports, not default exports (except page.tsx) - Handle errors with try/catch and return proper error responses
Avoid: - Do not use 'any' type — always define proper types - Do not use inline styles — use Tailwind classes - Do not create utility files for single-use functions - Do not add console.log to production code - Do not use deprecated Next.js APIs ```
This single rules file transforms Cursor's output from generic to project-aware.
Prêt à Maîtriser l'IA ?
Rejoignez 2 500+ professionnels qui ont transformé leur carrière avec le Bootcamp IA CodeLeap.
Advanced Rules: Context-Aware Generation
Rule file organization (`.cursor/rules/` directory):
`frontend.md` — Rules for React components: - Component structure (props type, early returns, main render) - State management patterns (when to use useState vs useReducer) - Data fetching patterns (server components vs client-side)
`backend.md` — Rules for API routes: - Request validation with Zod - Error response format - Authentication middleware pattern - Database query patterns
`testing.md` — Rules for tests: - Testing library preferences (Vitest, React Testing Library) - Test file naming conventions - Mock patterns - Minimum test coverage expectations
`git.md` — Rules for git operations: - Commit message format - Branch naming conventions - PR description template
Each file is loaded when Cursor works on files matching that domain, giving contextually relevant instructions.
Real Examples: Before and After Rules
Without rules — You ask Cursor to "add a new API route for user profiles": - Generic JavaScript (not TypeScript) - No error handling - No input validation - Default export - Inconsistent naming
With rules — Same request: - TypeScript with proper types - Zod validation on request body - try/catch with standardized error responses - Named export - Follows project naming conventions - Uses Prisma for database access - Includes proper HTTP status codes
The difference is dramatic. Rules don't just save you from fixing AI output — they let you trust AI output.
Pro tip: Evolve your rules over time. When you notice Cursor making a mistake repeatedly, add a rule to prevent it. After a few weeks, your rules file becomes a comprehensive coding standards document.
Cursor rules mastery is a core skill in CodeLeap's Developer Track. You'll create and refine rules files across multiple projects, learning which rules have the highest impact on code quality.