nestjs-autoloader
Version:
Module autoloading for NestJS
92 lines (67 loc) • 2.29 kB
Markdown
# NestJS Autoloader
[](https://github.com/LucaScorpion/nestjs-autoloader/actions/workflows/build.yml)
[](https://www.npmjs.com/package/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';
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';
export class YourModule {}
```
## Example
Before:
```typescript
import { Module } from '@nestjs/common';
export class AuthModule {}
```
After:
```typescript
import { AutoloadModule } from 'nestjs-autoloader';
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.