| name | mern-std |
| description | Coding standards for MERN Next.js apps in a pnpm monorepo. |
Purpose
Ensure consistent, maintainable code. Complements stack, security, and NFR skills.
Monorepo structure
apps/web/ # Next.js app
src/
app/ # Next.js app router (pages, layouts, API routes)
api/**/route.ts # API route handlers
page.tsx # Pages
layout.tsx # Layouts
components/ # React components
lib/ # Utilities
server/ # Server-only utilities
db/models/ # Mongoose models
packages/shared/ # Zod schemas, types, utilities
Important: All app router files MUST be in apps/web/src/app/, never in apps/web/app/.
Next.js with --src-dir expects the app directory inside src/.
apps/web depends on packages/shared via workspace:*
- Feature-specific components live under the feature folder
Key conventions
Environment
- Never access
process.env directly in app code; use the env module
Schemas and types
- Define request/response schemas in
packages/shared
- Infer types from zod:
z.infer<typeof Schema> — don't duplicate interfaces
- Reuse schemas across client and server
API route handlers
- Validate input with zod at boundary
- Consistent JSON envelope for responses
- Safe error mapping (no stack traces to clients)
- Pagination for list endpoints (cursor or offset)
- Rate limiting on auth/expensive endpoints