Nest: Multer options neglected when using MulterModule like on the docs.

Created on 3 May 2019  路  7Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I followed the File Upload async configuration documentation

Expected behavior

Multer options should work, uploading a file exceeding the limit should not work, and I should see the log from fileFilter function each time I do an upload.

Minimal reproduction of the problem with instructions

The app module app.module.ts:

@Module({
  imports: [
    MulterModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (config: ConfigService) => ({
        fileFilter: (req, file, cb) => {
          console.log('********************', config.get('FILE_SIZE_LIMIT'));
          cb(null, true);
        },
        limits: {
          fileSize: config.get('FILE_SIZE_LIMIT'),
        },
      }),
      inject: [ConfigService],
    }),
  ]
})

The controller controller.ts

@Controller('upload')
export class FilesController {
  constructor(private readonly config: ConfigService) {}
  @Post()
  @UseInterceptors(FileInterceptor('file'))
  upload(@UploadedFile() file) {
    console.log(file);
    return 'Okay';
  }
}

When having multer options as the second argument of FileInterceptor everything works fine, but when using MulterModule just like on the docs, the options are neglected.

Environment


Nest version: 6.1.1

For Tooling issues:

  • Node version: 10.15.3
  • Platform: Linux
needs clarification common

Most helpful comment

I ran into a similar issue. I was trying to register multer in AppModule, but expected the settings there to be used in a feature module. The multer options get consumed while the feature module is being created, so anything set in the AppModule is ignored. Not sure if this is by design or a bug, but moving the multer module import to the feature module fixed it for me.

Can we get an update on this? I just ran into it as an issue. Seems like this is something that you'd only want to define at the AppModule like with database and not need to copy into every module where you'll use multer?

All 7 comments

Please, provide a minimal repository which reproduces your issue :)

I ran into a similar issue. I was trying to register multer in AppModule, but expected the settings there to be used in a feature module. The multer options get consumed while the feature module is being created, so anything set in the AppModule is ignored. Not sure if this is by design or a bug, but moving the multer module import to the feature module fixed it for me.

I am trying to do the same thing, except that I dont need 'config' module - in my case I need to query database and I want to use another module for that. But I cannot make it to work in any modules except AppModule, Nest produces error like this:

Nest can't resolve dependencies of the MULTER_MODULE_OPTIONS (?). Please make sure that the argument at index [0] is available in the MulterModule context. +424ms Error: Nest can't resolve dependencies of the MULTER_MODULE_OPTIONS (?). Please make sure that the argument at index [0] is available in the MulterModule context.

import { Module } from '@nestjs/common';
import { UploadController } from './upload.controller';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { ProjectModule } from '../project/project.module';
import { ProjectService } from '../project/project.service';

@Module({
  imports: [MulterModule.registerAsync({
    useFactory: async (projectService: ProjectService) => ({
      imports: [ProjectModule],
      storage: diskStorage({
        destination: async (req, file, cb) => {
          const p = await projectService.findOne(req.params.id);
          console.log(p);
          //here we will get folder name from p...
          return cb(null, `${__dirname}/../../data/project/${req.params.id}`)
        },
        filename: (req, file, cb) => {
          return cb(null, file.originalname);
        }
      })
    }),
    inject: [ProjectService]
  })],
  controllers: [UploadController],
  providers: [ProjectService],
})
export class UploadModule {}

I ran into a similar issue. I was trying to register multer in AppModule, but expected the settings there to be used in a feature module. The multer options get consumed while the feature module is being created, so anything set in the AppModule is ignored. Not sure if this is by design or a bug, but moving the multer module import to the feature module fixed it for me.

Can we get an update on this? I just ran into it as an issue. Seems like this is something that you'd only want to define at the AppModule like with database and not need to copy into every module where you'll use multer?

Got it! Put the inject before the useFactory and voila, it works!

MulterModule.registerAsync({
  imports: [ ConfigModule ],
  inject: [ ConfigService ],
  useFactory: async (configService: ConfigService) => {
    return {
      storage: diskStorage({
        destination: async (req, file, cb) => {
          const path: string = configService.get('staticPath');
          console.log(path);
          return cb(null, path);
        },
        filename: (req, file, cb) => {
          return cb(null, `${Date.now()}_${file.originalname}`);
        }
      })
    }
  },
}),

I ran into a similar issue. I was trying to register multer in AppModule, but expected the settings there to be used in a feature module. The multer options get consumed while the feature module is being created, so anything set in the AppModule is ignored. Not sure if this is by design or a bug, but moving the multer module import to the feature module fixed it for me.

Experiencing the same issue. Multer module registration ignored in the AppModule and works in the feature module. Is this a bug?

Please, check the documentation. MulterModule is not a global module https://docs.nestjs.com/modules

Was this page helpful?
0 / 5 - 0 ratings

Related issues

2233322 picture 2233322  路  3Comments

menme95 picture menme95  路  3Comments

mishelashala picture mishelashala  路  3Comments

anyx picture anyx  路  3Comments

breitsmiley picture breitsmiley  路  3Comments