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"]
}