Why Use Modules?
Modules help you:- Organize code by feature instead of by type
- Scale large applications with many features
- Group related files together for better maintainability
- Create clear boundaries between different parts of your application
Installation
Creating a Module
Generate a new module
Use the This creates a new module directory in the
make:module command to create a new module:app/ folder with the following structure:Generating Files Inside a Module
After creating a module, you can use standard AdonisJSmake commands with the --module or -m flag to generate scoped resources:
Module Structure Example
The starter kit comes with several pre-built modules. Here’s the structure of theusers module:
Module Routes
Each module can have its ownroutes.ts file to define module-specific routes:
app/users/routes.ts
Best Practices
Keep modules focused
Each module should represent a single feature or domain concept. For example:
users, auth, billing, analytics.Use path aliases
Always use the generated path aliases (e.g.,
#users/*) instead of relative paths for better maintainability.Organize by feature, not by type
Instead of having all controllers in one place, group related controllers, models, and services together in a module.
Keep shared code separate
If code is used across multiple modules, consider placing it in a common module or shared utilities folder.
Example Modules in the Starter Kit
The starter kit includes these modules out of the box:- auth - Authentication and authorization
- users - User management and profiles
- common - Shared utilities and components
- core - Core application functionality
- marketing - Marketing pages and content
- analytics - Analytics and tracking