Namespace 'global.Express' has no exported member 'Multer'
Asked Answered
A

4

22

Namespace 'global.Express' has no exported member 'Multer'. Been straggling with this error for 2 days now. I've tried:

  • import "multer"
  • import { Multer } from "multer"
  • import { Express } from "express"
  • Add tsconfig types: ["Multer"]

and yet my backend keeps erroring on build.

code example

Abortionist answered 9/3, 2023 at 17:49 Comment(0)
B
21

I solved the same issue by adding @types/multer to my dependencies.

So either yarn add @types/multer or npm install --save @types/multer

Bloodshot answered 13/3, 2023 at 23:4 Comment(0)
Z
16

After installing npm install -D @types/multer, add "Multer" to compilerOptions --> types property inside your tsconfig.json file:

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "types": ["node", "Multer"]     // <-- Add "Multer" here
  }
}

Explanation: In your tsconfig.json file, possibly there's a "types" property specified under compilerOptions, which, according to this typescript definition, if types is specified, only packages listed will be included in the global scope, therefore, if "Multer" was not included there, it won't automatically be included in the global scope, and this is why you're getting an error Namespace 'global.Express' has no exported member 'Multer'.

Shortcut hack:

Warning: This is only a hack to make Multer available in the Express namespace, and you should make sure typings are available to typescript like I explained above.

import 'multer'; // a hack to make Multer available in the Express namespace

// ...

async createNewPost(
    // ...
    @UploadedFile() file: Express.Multer.File,
    // ...
)

PS: In production, you may need to move @types/multer from devDependencies to dependencies for an unclear reason.

PS2: If you're working with an nx.workspace, make sure to edit the "types" inside tsconfig.app.json under the Nest (API) folder.

tsconfig.app.json

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "outDir": "../../dist/out-tsc",
        "module": "commonjs",
        "types": ["node", "Multer"],    // <-- Add "Multer" here
        "emitDecoratorMetadata": true,
        "target": "es2015"
    },
    "exclude": ["**/*.spec.ts", "**/*.test.ts"],
    "include": ["**/*.ts"]
}
Zachary answered 20/7, 2023 at 19:29 Comment(2)
For me, adding "multer" to tsconfig.json worked (notice the starting "M").Kristakristal
Pretty sure this mean we should be avoiding MulterAlps
O
1

If adding @types/multer and importing them not helping, try to change the typescript version in IDE. In VSCode Command Palette -> TypeScript: Select TypeScript version -> Use workspace version.

Overcrop answered 12/7, 2023 at 8:39 Comment(0)
S
1

In a word.

If you have install @types/multer, just import 'multer' in any project file.

Soble answered 28/5 at 3:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.