Skip to content

kirtanp04/create-mern-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MongoDBΒ  ExpressΒ  ReactΒ  Node.jsΒ  TypeScriptΒ  Vite

mern-builder

Scaffold a production-ready MERN stack app in seconds.
Like create-next-app β€” but for MERN + Vite + TypeScript.

npm version npm downloads Node.js License: MIT PRs Welcome


⚑ Quick Start

npx mern-builder my-app

No installation required. Just run and answer a few questions.

Or pass the project name later:

npx mern-builder          # interactive β€” prompts for name
npx mern-builder my-app   # name pre-filled, rest is interactive

🎬 What Happens

The CLI walks you through a fully interactive setup with section-by-section prompts. After answering all questions, you get a summary screen where you can go back and edit any section before the project is generated.

β—†  Project Setup
β—†  Frontend
β—†  Backend
β—†  DevOps & Tooling
β—†  Review your choices  ← edit anything before confirming
β—†  Scaffold!

πŸ›  What You Can Configure

Project Setup

Option Choices
Package manager npm Β· pnpm (recommended) Β· yarn
Install deps now yes / no
Init git repo yes / no

🎨 Frontend

Option Choices
UI Library Tailwind CSS Β· MUI v6 Β· shadcn/ui
Routing None Β· React Router v6 Β· TanStack Router
State management None Β· Zustand Β· Redux Toolkit Β· Jotai
Path alias @/ yes / no

πŸ”§ Backend

Option Choices
Database MongoDB (Mongoose) Β· PostgreSQL (Prisma) Β· MySQL (Prisma) Β· None
Authentication None Β· JWT Β· JWT + Refresh Token
CORS yes / no
Logger None Β· Pino Β· Winston
Security middleware Rate limiting Β· Helmet.js Β· Zod env validation

🐳 DevOps & Tooling

Option Choices
Docker None Β· Dockerfiles Β· docker-compose
Testing None Β· Vitest Β· Vitest + Supertest
Code quality ESLint + Prettier Β· Husky + lint-staged

πŸ“ Generated Structure

my-app/
β”œβ”€β”€ frontend/                   # Vite + React 18 + TypeScript
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/         # your UI components
β”‚   β”‚   β”œβ”€β”€ hooks/              # useApi, useLocalStorage
β”‚   β”‚   β”œβ”€β”€ pages/              # route-level page components
β”‚   β”‚   β”œβ”€β”€ routes/             # React Router / TanStack route files
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   └── api.ts          # axios client (with auth interceptors)
β”‚   β”‚   β”œβ”€β”€ store/              # Zustand / Redux / Jotai / Context
β”‚   β”‚   └── types/              # shared TypeScript types
β”‚   β”œβ”€β”€ vite.config.ts
β”‚   └── Dockerfile              # (if Docker selected)
β”‚
β”œβ”€β”€ backend/                    # Express + TypeScript
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”‚   β”œβ”€β”€ env.ts          # Zod-validated environment
β”‚   β”‚   β”‚   β”œβ”€β”€ database.ts     # MongoDB / Prisma connection
β”‚   β”‚   β”‚   └── cors.ts         # CORS options
β”‚   β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   β”‚   β”œβ”€β”€ errorHandler.ts # global error + Zod error handling
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.ts         # JWT authenticate + authorize
β”‚   β”‚   β”‚   └── rateLimiter.ts
β”‚   β”‚   β”œβ”€β”€ models/             # Mongoose models / Prisma schema
β”‚   β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── utils/
β”‚   β”‚       β”œβ”€β”€ logger.ts       # Pino / Winston
β”‚   β”‚       └── jwt.ts          # sign + verify helpers
β”‚   β”œβ”€β”€ prisma/                 # (if PostgreSQL / MySQL)
β”‚   └── Dockerfile              # (if Docker selected)
β”‚
β”œβ”€β”€ docker-compose.yml          # (if compose selected)
β”œβ”€β”€ .vscode/                    # editor settings + extension recommendations
└── package.json                # root workspace: dev, build, lint, test

πŸš€ What Gets Generated β€” Highlights

Frontend

  • Vite + React 18 with hot module replacement out of the box
  • TypeScript with strict mode and path aliases (@/ β†’ src/)
  • Axios API client β€” pre-configured with base URL, timeout, and optional JWT interceptors + silent refresh on 401
  • useApi hook β€” typed, reusable data-fetching hook with loading / error / data state
  • useLocalStorage hook β€” cross-tab synced with StorageEvent
  • Full UI library setup: MUI theming, shadcn/ui CSS variables + dark mode, or plain Tailwind

Backend

  • Express with express-async-errors β€” no manual try/catch in every route
  • Graceful shutdown handling SIGTERM / SIGINT
  • Global error handler β€” normalises AppError, ZodError, and unknown errors into a consistent JSON response
  • JWT auth with role-based authorize() middleware; refresh-token flow uses httpOnly cookies
  • Zod env validation β€” server refuses to start if .env is misconfigured
  • Structured logging β€” Pino with pino-pretty in dev and JSON + log-level routing in production; Winston with DailyRotateFile

DevOps

  • Multi-stage Dockerfiles β€” build stage + minimal production image with health-check
  • docker-compose β€” frontend (nginx), backend, and database service with health-checks
  • Nginx config with SPA routing and /api proxy

πŸ“¦ After Scaffolding

cd my-app

# 1. Configure environment
cp backend/.env.example backend/.env
# β†’ fill in DB connection string, JWT secrets, etc.

# 2. Start development servers (frontend + backend concurrently)
pnpm dev
#  Frontend β†’  http://localhost:5173
#  Backend  β†’  http://localhost:5000
#  API      β†’  http://localhost:5000/api/v1

# 3. Build for production
pnpm build

Prisma (PostgreSQL / MySQL only)

cd backend
pnpm db:generate   # generate Prisma Client
pnpm db:migrate    # run migrations
pnpm db:studio     # open Prisma Studio GUI

Docker

pnpm docker:build  # build images
pnpm docker:up     # start all services (detached)
pnpm docker:logs   # tail logs
pnpm docker:down   # stop services

🌐 API Endpoints

The generated backend exposes:

Method Endpoint Description Auth
GET /health Health check β€”
POST /api/v1/auth/register Register new user β€”
POST /api/v1/auth/login Login β€”
GET /api/v1/auth/me Get current user βœ… JWT
POST /api/v1/auth/refresh Refresh access token πŸͺ Cookie
POST /api/v1/auth/logout Logout βœ… JWT
GET /api/v1/users List users β€”
GET /api/v1/users/:id Get user by ID β€”

Some endpoints are only generated based on your auth strategy selection.


πŸ”‘ Environment Variables

The generated backend/.env.example includes everything you need:

PORT=5000
NODE_ENV=development
LOG_LEVEL=debug

# Database (one of the following)
MONGODB_URI=mongodb://localhost:27017/my-app
DATABASE_URL=postgresql://postgres:secret@localhost:5432/my-app?schema=public

# JWT (if auth selected)
JWT_SECRET=your-super-secret-jwt-key-min-32-chars
JWT_EXPIRES_IN=15m

# JWT Refresh (if jwt-refresh selected)
JWT_REFRESH_SECRET=your-refresh-secret-min-32-chars
JWT_REFRESH_EXPIRES_IN=7d

# CORS (if selected)
ALLOWED_ORIGINS=http://localhost:5173

⚠️ Always change the JWT secrets before deploying to production. Never commit your .env file.


πŸ§‘β€πŸ’» Develop the CLI Itself

git clone https://github.com/kirtanp04/create-mern-cli
cd mern-builder
npm install

# Run without building (ts-node)
npm run dev

# Build to dist/
npm run build

# Test locally
npm link
mern-builder test-project

🀝 Contributing

Contributions, issues, and feature requests are welcome!

  1. Fork the repo
  2. Create your branch: git checkout -b feat/my-feature
  3. Commit your changes: git commit -m 'feat: add my feature'
  4. Push to the branch: git push origin feat/my-feature
  5. Open a Pull Request

Please follow Conventional Commits for commit messages.


πŸ“‹ Requirements

Requirement Version
Node.js >= 18.0.0
npm / pnpm / yarn any recent version

πŸ“„ License

MIT Β© 2024 β€” made with β˜• and TypeScript.


About

npx mern-builder my-app

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors