Template Placeholders
Use these placeholders in your .stub files:
{{moduleName}}- users{{ModuleName}}- Users{{module_name}}- users{{module-name}}- users{{camelModuleName}}- users{{MODULE_NAME}}- USERS
Singular versions:
{{singularName}}- user{{SingularName}}- User{{singular_name}}- user{{singular-name}}- user{{camelSingularName}}- user{{SINGULAR_NAME}}- USER
Generated Structure
src/app/users/
├── users.module.ts
├── users.controller.ts
├── users.service.ts
├── dto/
│ ├── create-user.dto.ts
│ └── update-user.dto.ts
└── schema/
└── user.schema.ts
Custom Templates
You can customize any template by editing the .stub files in the templates/ directory after running nest-module init.
Customizing Existing Templates
- Run init to create default templates:
nest-module init
- Edit any
.stubfile in thetemplates/directory:
templates/
├── module.stub
├── controller.stub
├── service.stub
├── create-dto.stub
├── update-dto.stub
└── schema.stub
- Example: Customize the service template
Edit templates/service.stub - the file should contain EXACTLY this text with the double curly braces:
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { {{SingularName}} } from './schema/{{singularName}}.schema';
@Injectable()
export class {{ModuleName}}Service {
constructor(
@InjectModel({{SingularName}}.name)
private {{camelSingularName}}Model: Model<{{SingularName}}>,
) {}
// Add your custom methods here
async customMethod() {
// Your custom logic
}
}
Important: The placeholders like {{SingularName}} should be typed exactly as shown - with double curly braces. When you generate a module called "users", these will be automatically replaced:
- {{SingularName}} → User
- {{singularName}} → user
- {{ModuleName}} → Users
- {{camelSingularName}} → user
- Use placeholders anywhere in your templates:
- All placeholders listed in the "Template Placeholders" section work in any template
- Mix and match naming conventions as needed
- Add your own custom code around the placeholders
Adding New Custom Templates
- Create a new
.stubfile:
# Create a repository template
touch templates/repository.stub
- Add content with placeholders:
Create templates/repository.stub with EXACTLY this content:
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { {{SingularName}} } from './schema/{{singularName}}.schema';
@Injectable()
export class {{ModuleName}}Repository {
constructor(
@InjectModel({{SingularName}}.name)
private model: Model<{{SingularName}}>,
) {}
async findByCustomField(field: string) {
return this.model.find({ customField: field }).exec();
}
}
Note: Type the placeholders exactly as shown - {{SingularName}} with double curly braces. Your editor may show syntax errors (that's normal), but the generator will replace them correctly.
- Register in
generator.config.js:
module.exports = {
templates: {
// ... existing templates
repository: {
extension: 'repository.ts',
required: false,
outputPath: 'repositories',
filename: '{{singularName}}.repository.ts'
},
// Add as many custom templates as you need
interface: {
extension: 'interface.ts',
required: false,
outputPath: 'interfaces',
filename: '{{singularName}}.interface.ts'
}
},
// Optional: exclude custom templates for specific modules
exclusions: {
repository: ['auth', 'health'],
interface: ['logs']
}
};
- Generate modules with your custom templates:
nest-module generate products
Output:
src/app/products/
├── products.module.ts
├── products.controller.ts
├── products.service.ts
├── dto/
│ ├── create-product.dto.ts
│ └── update-product.dto.ts
├── schema/
│ └── product.schema.ts
├── repositories/
│ └── product.repository.ts
└── interfaces/
└── product.interface.ts
Template Customization Tips
- Keep it DRY: Use placeholders for all naming to avoid repetition
- Use exclusions: Skip certain templates for specific modules (e.g., no DTOs for auth)
- Test incrementally: Generate a test module after each template change
- Version control: Commit your
templates/directory to share with your team - Module-specific logic: Add conditional code in templates if needed
- Nested directories: Use
outputPathto organize files in subdirectories