express-modular-cli
v2.0.1
Published
An ultra-lightweight CLI tool with only 1 dependency to generate modular Express.js projects
Maintainers
Readme
Express Modular CLI
A powerful CLI tool for generating modular Express.js projects and components with TypeScript support.
📦 Installation
Install globally via npm to use the CLI from anywhere:
npm install -g express-modular-cli🚀 Quick Start
1. Create a New Project
exm-cli new my-express-app
cd my-express-appThis command will:
- ✅ Create a complete Express.js project structure
- ✅ Install all required dependencies
- ✅ Initialize git repository with initial commit
- ✅ Set up TypeScript configuration with path aliases
- ✅ Configure development and production scripts
2. Generate Components
Once inside a project, generate individual components:
# Generate a complete user module
exm-cli g module user
# Or generate individual components
exm-cli g controller user
exm-cli g service user
exm-cli g route user📚 Commands
new <project-name>
Creates a new Express.js project with modern architecture.
exm-cli new my-apiWhat it does:
- Downloads the latest Express.js boilerplate
- Sets up modular project structure
- Installs dependencies automatically
- Configures TypeScript with path aliases
- Initializes git repository
generate <type> <name> (alias: g)
Generates individual components within an existing project.
# Generate a controller
exm-cli generate controller product
exm-cli g controller product # Short alias
# Generate a service
exm-cli g service product
# Generate a complete module (all files)
exm-cli g module product🧩 Component Types
controller
Generates Express.js route controllers with CRUD operations.
exm-cli g controller userGenerated file: src/app/modules/user/user.controller.ts
Features:
- ✅ Complete CRUD operations (create, read, update, delete)
- ✅ Proper error handling with catchAsync
- ✅ Response formatting with sendResponse
- ✅ TypeScript types and interfaces
- ✅ Path aliases for clean imports
service
Creates business logic services with data access layer.
exm-cli g service userGenerated file: src/app/modules/user/user.service.ts
Features:
- ✅ Service layer pattern implementation
- ✅ Interface-based data operations
- ✅ Ready for database integration
- ✅ TypeScript support with proper typing
model
Generates data models with schema definitions.
exm-cli g model userGenerated file: src/app/modules/user/user.model.ts
Features:
- ✅ Mongoose schema definition
- ✅ Model methods and statics
- ✅ TypeScript interface integration
- ✅ Validation rules and middleware
route
Creates Express.js route definitions with middleware.
exm-cli g route userGenerated file: src/app/modules/user/user.route.ts
Features:
- ✅ RESTful route structure
- ✅ Controller method binding
- ✅ Middleware integration points
- ✅ Automatic route registration
validation
Generates input validation schemas.
exm-cli g validation userGenerated file: src/app/modules/user/user.validation.ts
Features:
- ✅ Zod validation schemas
- ✅ Request body validation
- ✅ Parameter validation
- ✅ Type-safe validation rules
interface
Creates TypeScript interfaces and types.
exm-cli g interface userGenerated file: src/app/modules/user/user.interface.ts
Features:
- ✅ TypeScript interface definitions
- ✅ Data transfer objects (DTOs)
- ✅ Type exports for reusability
- ✅ Model integration types
module
Generates all component files for a complete module.
exm-cli g module userGenerated files:
user.controller.ts- Route controllersuser.service.ts- Business logicuser.model.ts- Data modelsuser.route.ts- Route definitionsuser.validation.ts- Input validationuser.interface.ts- TypeScript types
Additional features:
- ✅ Automatic route registration in main router
- ✅ All files properly interconnected
- ✅ Ready-to-use module structure
🏗️ Project Structure
The CLI generates projects with a clean, modular architecture:
my-express-app/
├── src/
│ ├── app/
│ │ ├── modules/
│ │ │ └── user/
│ │ │ ├── user.controller.ts
│ │ │ ├── user.service.ts
│ │ │ ├── user.model.ts
│ │ │ ├── user.route.ts
│ │ │ ├── user.validation.ts
│ │ │ └── user.interface.ts
│ │ ├── routes/
│ │ │ └── index.ts # Route aggregation
│ │ ├── utils/
│ │ │ ├── catchAsync.ts # Error handling
│ │ │ ├── sendResponse.ts # Response formatting
│ │ │ └── ... # Other utilities
│ │ └── app.ts # Express app configuration
│ └── server.ts # Server startup
├── package.json
├── tsconfig.json # TypeScript config with path aliases
├── .env.example
├── .gitignore
└── README.md⚙️ Smart Features
Context-Aware Generation
The CLI automatically detects your project environment:
Inside Project:
- Uses existing project utilities (
catchAsync,sendResponse) - Imports with path aliases (
@/app/utils/...) - Integrates with existing route structure
Outside Project:
- Includes inline utility functions
- Works as standalone files
- No external dependencies required
Path Alias Support
Generated code uses modern TypeScript path aliases:
// Clean imports with path aliases
import catchAsync from '@/app/utils/catchAsync';
import { sendResponse } from '@/app/utils/sendResponse';
// Module-level relative imports
import { UserService } from './user.service';Automatic Route Registration
When generating modules or routes, the CLI automatically updates the main router:
// Automatically added to src/app/routes/index.ts
import { UserRoutes } from "../modules/user/user.route";
const moduleRoutes = [
{ path: "/user", route: UserRoutes },
// Your new routes are automatically registered here
];🛠️ Prerequisites
- Node.js: Version 16.0.0 or higher
- npm: Comes with Node.js
- Git: Required for project creation (downloads boilerplate)
🔍 Validation & Error Handling
The CLI includes robust validation:
Project Structure Validation
❌ Error: Not in a valid Express project directory
Missing required structure:
- src/app
- src/app/modules
- src/app/routes
Suggestions:
• Create a new project first using: exm-cli new <project-name>
• Or run this command from within an existing Express project directoryComponent Existence Check
❌ Error: controller user already existsInput Validation
- Project names must be valid npm package names
- Component names must be valid identifiers
- Component types must be supported
🚨 Troubleshooting
Command Not Found
# Check if installed globally
npm list -g express-modular-cli
# If not found, reinstall
npm install -g express-modular-cliPermission Issues
# On macOS/Linux, you might need sudo
sudo npm install -g express-modular-cli
# Or configure npm to use a different directory
npm config set prefix ~/.localGit Issues
# Ensure git is installed and configured
git --version
git config --global user.name "Your Name"
git config --global user.email "[email protected]"Network Issues
If project creation fails:
- Check internet connection
- Verify access to GitHub
- Try again (temporary network issues)
💡 Tips & Best Practices
1. Always Create Project First
# ✅ Correct workflow
exm-cli new my-api
cd my-api
exm-cli g controller user
# ❌ Wrong - will generate standalone files
exm-cli g controller user # Outside project2. Use Module Generation for Complete Features
# ✅ Generates all related files at once
exm-cli g module user
# ⚠️ Manual approach (more work)
exm-cli g controller user
exm-cli g service user
exm-cli g model user
exm-cli g route user
exm-cli g validation user
exm-cli g interface user3. Follow RESTful Naming
# ✅ Good naming
exm-cli g module user
exm-cli g module product
exm-cli g module order
# ❌ Avoid plurals in component names
exm-cli g module users # Will create usersController, etc.📋 Examples
Creating a Blog API
# 1. Create the project
exm-cli new blog-api
cd blog-api
# 2. Generate core modules
exm-cli g module user
exm-cli g module post
exm-cli g module comment
# 3. Generate additional components
exm-cli g controller auth
exm-cli g service emailCreating an E-commerce API
# 1. Create the project
exm-cli new ecommerce-api
cd ecommerce-api
# 2. Generate main modules
exm-cli g module product
exm-cli g module category
exm-cli g module order
exm-cli g module user
exm-cli g module cart📄 License
MIT License - see LICENSE file for details.
🐛 Issues & Support
Found a bug or need help? Create an issue on GitHub.
Happy coding! 🚀 Build amazing Express.js APIs with the power of modular architecture.
