🚀 MERN Scaffold
A powerful CLI tool to automate the generation of controllers and routes for MERN stack backend models — because scaffolding should be fast and effortless.
📦 Local Installation and Usage
npm i mern-d-scaffold
npx mern-d-scaffold generate model
📦 Global Installation and usage
npm install -g mern-d-scaffold
mern-d-scaffold generate model
⚙️ Prerequisites
Install express-async-handler for proper error handling:
npm install express-async-handler
🛠️ Usage
1. Create a Mongoose Model
Example: models/order.js
const mongoose = require('mongoose');
const orderSchema = new mongoose.Schema({
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
totalPrice: { type: Number, required: true },
status: { type: String, default: 'pending' },
});
module.exports = mongoose.model('Order', orderSchema);
2. Run the Scaffold CLI
mern-scaffold generate order
This command will:
✅ Check if models/order.js exists
✅ Generate controllers/orderController.js with full CRUD operations
✅ Generate routes/orderRoute.js
✅ Automatically register the route in index.js or server.js
📁 Output Example
controllers/orderController.js
Includes:
- Create Order
- Get All Orders
- Get Order by ID
- Update Order
- Delete Order
routes/orderRoute.js
- RESTful routes using Express, linked to the controller.
index.js
(automatically updated)
const orderRoutes = require('./routes/orderRoute');
app.use('/api/orders', orderRoutes);
✨ Features
- 🔧 Auto-generates boilerplate CRUD logic
- 📁 Automatically creates
controllers/
and routes/
directories if they don't exist
- 🔌 Dynamically updates main file with new routes
- 🧼 Clean formatting with proper line spacing and comments
- 💻 Cross-platform support (Windows, macOS, Linux)
❗ Troubleshooting
Issue | Solution |
---|
Model file not found | Ensure models/<model>.js exists before generating |
Main file not updated | Rename your main file to index.js or server.js , or manually add route |
Missing dependency | Run npm install express-async-handler |
Permission issues (Windows) | Run PowerShell as Admin: Start-Process powershell -Verb runAs |
🧪 Development
git clone https://github.com/your-username/mern-scaffold.git
cd mern-scaffold
npm install
npm link
After testing, remove the local link:
npm unlink mern-scaffold
License and Author Sections
📜 License
MIT License. See LICENSE
for details.
👨💻 Author
Dilukshan Viyapury
Feel free to contribute or open issues — let's build something awesome together!