Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace 'global.Express' has no exported member 'Multer'

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

like image 496
xarielah Avatar asked Aug 31 '25 16:08

xarielah


1 Answers

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"]
}
like image 113
Lorraine R. Avatar answered Sep 04 '25 00:09

Lorraine R.