AGENTS.md template for NestJS
A ready AGENTS.md for a NestJS API in TypeScript. It pins down the module layout, the test and build commands, and the dependency injection mistakes agents make.
# AGENTS.md A NestJS API written in TypeScript, organised as one module per feature and tested with Jest. ## Commands - `npm run start:dev`: start the API with reload on http://localhost:3000 - `npm run build`: compile to `dist/` - `npm run lint`: ESLint - `npm test`: unit tests - `npm run test:e2e`: end-to-end tests ## Project structure - `src/main.ts`: bootstrap and global pipes - `src/app.module.ts`: root module - `src/<feature>/`: one folder per feature: module, controller, service - `src/<feature>/dto/`: request and response DTOs - `test/`: end-to-end tests ## Rules for agents - Dependencies arrive through the constructor. Never create a provider with `new`. - Register a new provider in its module, and export it if another module uses it. - Every request body, query and param set has a DTO class with `class-validator` decorators. No `any`. - Do not use `forwardRef` to fix a circular import. Stop and report the cycle. ## Conventions ### NestJS - Module-per-feature. Controllers are thin; logic lives in providers/services. - Use DTOs + `class-validator` / `class-transformer` with the global `ValidationPipe`. - Dependency injection via the DI container — no `new Service()` in controllers. ### TypeScript - Prefer `type` for object shapes, `interface` only when extending third-party types. - Never use `any` — use `unknown` and narrow, or write a proper type. - No non-null assertions (`!`) except in tests or when a type guard just ran. - Use `readonly` for props, return types, and array params that don't mutate. - Prefer discriminated unions over enums. Enums only for stable public APIs. - Import types with `import type { ... }` to avoid runtime import cost. ### Jest - One behavior per test. Use `describe` sparingly. - `jest.useFakeTimers()` for time-dependent code; always restore. - Avoid snapshot tests for anything that isn't a pure renderer output. ## Avoid - Declaration-merging global namespaces. - Using `Function`, `Object`, or `{}` as types. ## Guardrails - Never commit secrets, API keys, or credentials. If you spot one in a diff, stop and flag it. - Do not invent dependencies. If a package is not already in the manifest, confirm before adding it. - When a command fails, read the actual error. Do not retry blindly or bypass with `--force`. - Prefer editing existing files over creating new ones. - Run the tests and the linter before saying a task is done, and say what you ran.
The commands and folders are the common defaults for NestJS. Change them to match your project before you commit the file: an agent will run exactly what is written here.
How to use it
Save the file as AGENTS.md in the root of your repository and commit it. Then check each tool you use:
| Tool | What to do |
|---|---|
| Claude Code | Reads AGENTS.md only when no CLAUDE.md exists in or above the folder. If you have a CLAUDE.md, put @AGENTS.md on its first line. |
| Cursor | Reads AGENTS.md from the project root, in the desktop app and the terminal. In the app, rules in .cursor/rules must be .mdc files. The terminal reads only plain .md files there. |
| Codex | Reads AGENTS.md from the root down to the folder you are in, with a shared 32 KiB limit across all of them. |
| GitHub Copilot | Reads AGENTS.md on some surfaces only. Keep a .github/copilot-instructions.md as well if your team relies on Copilot. |
Details and sources: the AGENTS.md compatibility matrix.
What agents get wrong in NestJS projects
These are the reasons the “Rules for agents” section says what it says.
Creating services with new
When injection fails, agents instantiate the class by hand. It compiles, but that copy has none of its own dependencies and tests can no longer swap it out.
Forgetting to register the provider
A new service that is not listed in its module, or not exported to the module that uses it, produces the "Nest can't resolve dependencies" error. Agents then shuffle imports at random until it goes away.
Skipping DTOs
Agents type a body as any or an inline object, so the global validation pipe has nothing to check and bad input reaches the service.
Patching circular imports with forwardRef
It hides the problem. Two modules that need each other usually means a third module is missing.
Questions
Where does AGENTS.md go in a NestJS project?
In the root of the repository, next to your manifest file. Tools read it from the folder you start them in and, for most tools, from parent folders too.
Does Claude Code read AGENTS.md?
Only when there is no CLAUDE.md in or above the working folder. If you already have a CLAUDE.md, keep it and make its first line @AGENTS.md so both tools read the same rules.
How long should the file be?
Short enough that every line is something the agent would get wrong without it. Codex shares a 32 KiB budget across all the AGENTS.md files it loads, and long files get followed less closely by every tool.
Should database rules go in this file?
Yes. Add one short section naming the ORM (TypeORM, Prisma or Drizzle), where entities or the schema live, and the migration command. The generator can add those rules for you.