Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lucascorpion/nestjs-autoloader
Simplify your NestJS modules by automatically loading providers and controllers.
https://github.com/lucascorpion/nestjs-autoloader
autoloader module nestjs
Last synced: about 2 months ago
JSON representation
Simplify your NestJS modules by automatically loading providers and controllers.
- Host: GitHub
- URL: https://github.com/lucascorpion/nestjs-autoloader
- Owner: LucaScorpion
- License: mit
- Created: 2023-02-02T19:30:37.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-15T18:31:35.000Z (5 months ago)
- Last Synced: 2024-11-25T12:03:44.469Z (about 2 months ago)
- Topics: autoloader, module, nestjs
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/nestjs-autoloader
- Size: 110 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# NestJS Autoloader
[![Build](https://github.com/LucaScorpion/nestjs-autoloader/actions/workflows/build.yml/badge.svg)](https://github.com/LucaScorpion/nestjs-autoloader/actions/workflows/build.yml)
[![NPM bundle size](https://img.shields.io/bundlephobia/min/nestjs-autoloader)](https://www.npmjs.com/package/nestjs-autoloader)[![npm](https://npmbadge.com/npm/nestjs-autoloader)](https://www.npmjs.com/package/nestjs-autoloader)
Simplify your NestJS modules by automatically loading providers and controllers.
# Installation
```shell
npm i nestjs-autoloader
```## Nest Compatibility
NestJS Autoloader works with Nest `v8.2.4` and up.
# Usage
```typescript
import { AutoloadModule } from 'nestjs-autoloader';@AutoloadModule(__dirname)
export class YourModule {}
```The first argument of the `AutoloadModule` decorator should always be `__dirname`,
this is the directory it will read and load files from.
The second argument is optional, and is the same as for a normal `Module`.
Here you can specify imports, additional providers, etc.
For example:```typescript
import { AutoloadModule } from 'nestjs-autoloader';@AutoloadModule(__dirname, {
imports: [OtherModule],
providers: [NonAutoloadedProvider]
})
export class YourModule {}
```## Example
Before:
```typescript
import { Module } from '@nestjs/common';@Module({
controllers: [AuthController],
providers: [AuthService, AdminService],
imports: [TypeOrmModule.forFeature([Admin])],
exports: [AuthService],
})
export class AuthModule {}
```After:
```typescript
import { AutoloadModule } from 'nestjs-autoloader';@AutoloadModule(__dirname, {
imports: [TypeOrmModule.forFeature([Admin])],
exports: [AuthService],
})
export class AuthModule {}
```## Nested Module Directories
The autoloader is designed to work with nested module directories.
For example:```
parent/
├── parent.module.ts
├── parent.service.ts
└── sub/
├── sub.module.ts
└── sub.service.ts
```This will load the `parent.service.ts` for the `parent` module,
but not the `sub.service.ts`.
The autoloader recognises nested modules by looking at `*.module.ts` files.
If a directory contains a file with that name,
it will exclude this directory from autoloading for the containing module.