Express' has no exported member 'Multer'
Express' has no exported member 'Multer'.
How do you use this library?
import { Express } from 'express';
type FileObj = Express.Multer.File & { ...}
Express' has no exported member 'Multer'.
din you install the multer typings as well?
npm install --save-dev @types/multer
din you install the multer typings as well?
npm install --save-dev @types/multer
I did. Didn't help
I did. Didn't help
Can you give us a bit more detail?
- package.json
- imports
- how you use it
Otherwise, all I can say is that it should work :)
I was facing the same issue, but can get it to compile using:
import { Express, Request } from 'express';
// This is a hack to make Multer available in the Express namespace
import { Multer } from 'multer';
type File = Express.Multer.File;
const file: File = req.file;
In devDependencies
"@types/express": "~4.17.3",
"@types/multer": "~1.4.5",
"typescript": "~4.1.2"
I'm encountering this issue too. Why is this made into a global declaration and not exported so we can explicitly import the types?
I did. Didn't help
Can you give us a bit more detail?
- package.json
- imports
- how you use it
Otherwise, all I can say is that it should work :)
controller
import { Express } from 'express';
import { FileInterceptor } from '@nestjs/platform-express';
...
@Post()
@UseInterceptors(FileInterceptor('image'))
uploadFile(
@Req() request: Request,
// Here is it
@UploadedFile() file: Express.Multer.File
) {
return this.fileUploadService.uploadFile(file);
}
package.json
"@types/multer": "^1.4.5",
It does compile successfully, uploading works etc, but vscode gives this error
// This is a hack to make Multer available in the Express namespace import { Multer } from 'multer';
Worked for me. Thanks
// This is a hack to make Multer available in the Express namespace import { Multer } from 'multer';Worked for me. Thanks
I'm glad this works, but I have no idea why it's needed.
I'm also using nestjs and vscode, but for me it works without the extra import...
import { Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
@Post('upload-image')
@UseInterceptors(FileInterceptor('file'))
async uploadImage(
@UploadedFile() file: Express.Multer.File,
): Promise<CdnFileDto> {
return this.cdnService.uploadFile(file);
}
perhaps the difference is that I don't import Express anywhere
// This is a hack to make Multer available in the Express namespace import { Multer } from 'multer';Worked for me. Thanks
I'm glad this works, but I have no idea why it's needed.
I'm also using nestjs and vscode, but for me it works without the extra import...
import { Post, UploadedFile, UseInterceptors } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; @Post('upload-image') @UseInterceptors(FileInterceptor('file')) async uploadImage( @UploadedFile() file: Express.Multer.File, ): Promise<CdnFileDto> { return this.cdnService.uploadFile(file); }perhaps the difference is that I don't import
Expressanywhere
When I don't import Express it says that it doesn't know what is express.
import { FileInterceptor } from '@nestjs/platform-express';
...
@Post()
@UseInterceptors(FileInterceptor('image'))
uploadFile(
@Req() request: Request,
// Here is it
@UploadedFile() file: Express.Multer.File
) {
return this.fileUploadService.uploadFile(file);
}
Cannot find namespace 'Express'.ts(2503)
// This is a hack to make Multer available in the Express namespace import { Multer } from 'multer';Worked for me. Thanks
This worked for me as well but I'd like to avoid doing a hack. Is there any reason why this is happening? In my case the problem arises when using ts-node.
I have added it in the types field of the compilerOptions in my tsconfig.json so it won't get removed as unused import from my ide.
{
"compilerOptions": {
...
"types": ["Multer"]
},
...
}
I had the same issue in nestjs but only during production build. Turns out, when I moved the @types/multer from devDependencies to dependencies it worked 🤷🏼♂️
I have added it in the
typesfield of thecompilerOptionsin mytsconfig.jsonso it won't get removed as unused import from my ide.{ "compilerOptions": { ... "types": ["Multer"] }, ... }
@gkTim keep in mind by doint it this why, all other global types that would be imported automatically won't this way except for Multer https://www.typescriptlang.org/tsconfig#types
All you have to do is:
import { Express } from 'express';
import 'multer';
This way you will also avoid any eslint warns about Multer never being used.
All you have to do is install it types yarn add @types/multer
This worked for me
None of the above solutions worked for me. What did work however was calling the import something else:
import whatever from 'multer';
This unexpected behavior is still present in all latest versions (NestJS, TypeScript, express, ...).
The correct way of doing it is to just add @types/multer to your dev dependencies.
In our team everything worked fine locally, without any imports and on the production server it threw the error.
It seems to be dependent on something about the environment.
import 'multer';
Did the trick for us and we preferred it, since it is the shortest solution.
One more thing to note: it can also help to move @types/multer from dev dependencies to dependencies. Why? Don't know.
For me it was just an "VS Code gone wild thing"
type this
import { Multer } from "multer";
then Ctrl + click on Multer to open the index.d.ts file and VS Code now learns that there is in fact Multer in Express Namespace :)
I'm having the same problem, but weirder. The code did compile ok, but adding unrelated code breaks it.
So I guess from past experiences, that there's a circular dependency happening somewhere. I didn't manage to solve it yet.
Fixed by running npm install -D @types/multer
Our team had same issue, did the npm i -D @types/multer and it didn't fix.
Turns out our local environment was running in Docker and the way it was set up wasn't recreating the node_modules on subsequent npm install {whatever}.
So we had to delete the local container and rebuild. Just in case someone else is having the same issue :)
import "multer";
...should be sufficient.
But I wonder if multer should even be messing around with express' declarations.
Consider:
import { Express } from "express";
let r: Express.Request;
const f = r.file.fieldname; // <-- Property 'file' does not exist on type 'Request'. ts(2339)
and
import { Express } from "express";
import "multer";
let r: Express.Request;
const f = r.file.fieldname; // <-- OK
...it feels pretty weird that Express.Request is changing depending on what else I import.
For yarn user, I use this yarn add @types/multer -D
clean, encapsuled, global
index.d.ts:
/* eslint-disable @typescript-eslint/no-empty-interface */
import { Multer as MulterNamed } from 'multer';
declare global {
namespace Express {
interface Multer extends MulterNamed {}
}
}
yarn add @types/multer -D and import multer; fixed it for me
yarn add @types/multer -Dandimport multer;fixed it for me
This worked for me too.
I actually, I did some inspection and found out that they're 03 namespaces declaration of Express.
- @types/express-session
- @types/multer
- @types/passport
To my understanding,
Expressfrommulterwasn't the priority so addingimport 'multer'prioritize it over the others.
yarn add @types/multer -Dandimport multer;fixed it for me
it works for me thanks
Awesome.
I was facing the same issue, but can get it to compile using:
import { Express, Request } from 'express'; // This is a hack to make Multer available in the Express namespace import { Multer } from 'multer'; type File = Express.Multer.File; const file: File = req.file;In devDependencies
"@types/express": "~4.17.3", "@types/multer": "~1.4.5", "typescript": "~4.1.2"
This doesn't work for me because I have prettier. It keeps removing unused imports.
Here is mine
import type { Multer } from 'multer';
type File = Multer;
"types": ["Multer"]
Perfect, this solved it for me.