Nest: Multer (File Upload) is not working globally since v.6.3.1

Created on 11 Jun 2019  路  9Comments  路  Source: nestjs/nest

Regression

Potential Commit/PR that introduced the regression**

https://github.com/nestjs/nest/commit/f210ebca2fddecef6b235547498b46fe76b3b9e5

Describe the regression

I use Multer for File Upload. Before v.6.3.1 I imported MulterModule in AppModule only, it worked globally. After v.6.3.1, it works not globally anymore, at least, it's options (destination) is not gobally. I need to import MulterModule in any Module where I upload files now.

Input Code

https://github.com/hellokvn/nest-multer

  1. Upload "file" to: PUT http://localhost:3000
    file path is correct (see console log)

  2. Upload "file" to: PUT http://localhost:3000/lorem
    file path is incorrect (see console log)

Expected behavior/code

I expect to import MulterModule only once, as long it's inside AppModule. Not sure, if my expectation is like it really should be. If I am wrong, it would be nice to know how I can use MulterModule globally.

Environment

Nest version: 6.3.1

For Tooling issues:

  • Node version: 10.14.1
  • Platform: Windows 10
needs triage type

All 9 comments

@hellokvn thanks so much for that! How did you figure this one out?

It was never supposed to work globally.

I expect to import MulterModule only once, as long it's inside AppModule. Not sure, if my expectation is like it really should be. If I am wrong, it would be nice to know how I can use MulterModule globally.

Use @Global() decorator

@hellokvn
How did you resolve this?
All links on this issue is unavailable now :(

Here i have the same problem

Use @Global() decorator

If it doesn't work, please link me a repository. I will try to fix it then.

Sorry about that, i found the solution:

After put @Global and configure i need includes MulterModule on exports in app.module:

@Global()
@Module({
    imports: [
        MulterModule.register({
            storage: diskStorage({
                destination: './uploads',
                filename: (req, file, cb) => {
                    const randomName = Array(32).fill(null).map(() => (Math.round(Math.random() * 16)).toString(16)).join('')
                    return cb(null, `${randomName}${extname(file.originalname)}`)
                }
            })
        }),
        ...
    ],
    controllers: [AppController],
    providers: [AppService],
    exports: [MulterModule]
})
export class AppModule { }

commented here

Great! Personally, I would recommend to put the MulterModule into some ConfigModule which you should make global and import it into AppModule usually. That's how I do it.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marshall007 picture marshall007  路  3Comments

2233322 picture 2233322  路  3Comments

KamGor picture KamGor  路  3Comments

artaommahe picture artaommahe  路  3Comments

anyx picture anyx  路  3Comments