Skip to content

MulterModuleAsyncOptions

reference
2 min readUpdated

Kind: Interface

Source: packages/platform-express/multer/interfaces/files-upload-module.interface.ts

Part of: Platform Express

MulterModuleAsyncOptions configures Multer asynchronously when registering NestJS file upload support. It allows upload options to be provided by an existing provider, a dedicated options factory class, or an inline factory function with dependency injection.

Properties

PropertyType
useExistingType<MulterOptionsFactory>
useClassType<MulterOptionsFactory>
useFactory`( ...args: any[] ) => Promise
injectany[]

Diagram

mermaid
graph LR
  A[MulterModuleAsyncOptions] --> B[useExisting]
  A --> C[useClass]
  A --> D[useFactory]
  A --> E[inject]

  B --> F[MulterOptionsFactory]
  C --> F
  D --> G[MulterModuleOptions]
  E --> D
  F --> G
  G --> H[MulterModule.registerAsync]

Usage

ts
import { Module } from '@nestjs/common';
import {
  MulterModule,
  MulterModuleAsyncOptions,
  MulterOptionsFactory,
} from '@nestjs/platform-express';
import { ConfigService } from '@nestjs/config';
import { diskStorage } from 'multer';

const multerOptions: MulterModuleAsyncOptions = {
  inject: [ConfigService],
  useFactory: async (configService: ConfigService) => ({
    storage: diskStorage({
      destination: configService.get<string>('UPLOAD_DIRECTORY', './uploads'),
      filename: (_request, file, callback) => {
        callback(null, `${Date.now()}-${file.originalname}`);
      },
    }),
    limits: {
      fileSize: 5 * 1024 * 1024,
    },
  }),
};

@Module({
  imports: [MulterModule.registerAsync(multerOptions)],
})
export class AppModule {}

AI Coding Instructions

  • Use exactly one configuration strategy: useExisting, useClass, or useFactory.
  • Add required dependencies to inject when using useFactory; their order must match the factory function parameters.
  • Return a valid MulterModuleOptions object from the factory, either synchronously or as a Promise.
  • Prefer useExisting when an existing MulterOptionsFactory provider should be reused; use useClass when Nest should create a dedicated factory provider.
  • Configure storage, file-size limits, and file filtering in the returned Multer options rather than directly in controllers.

How it works

MulterModuleAsyncOptions is the public TypeScript configuration shape accepted by MulterModule.registerAsync(). It extends only the imports member of ModuleMetadata; its remaining members select how the module resolves MulterModuleOptions. MulterModuleOptions is an alias for MulterOptions. files-upload-module.interface.ts:4 files-upload-module.interface.ts:16-25 multer.module.ts:30

useFactory takes precedence when it is set: the options-provider construction checks it first. A set useExisting also prevents registration of useClass; the injected factory token then selects useExisting before useClass. multer.module.ts:48-49 multer.module.ts:63-75

All members declared directly by this interface are optional, and the visible implementation contains no explicit validation or thrown error for a configuration with none of useFactory, useExisting, or useClass. In that case, its class branch constructs a provider whose token and injection entry are undefined. files-upload-module.interface.ts:20-25 multer.module.ts:48-57 multer.module.ts:70-75

Calling registerAsync constructs a dynamic module that exports MULTER_MODULE_OPTIONS and adds a generated MULTER_MODULE_ID value provider. multer.module.ts:30-42

Was this page helpful?

Download as PDF
MulterModuleAsyncOptions — NestJS head-to-head