英文:
Namespace 'global.Express' has no exported member 'Multer'
问题
Namespace 'global.Express'没有导出成员'Multer'。我已经为这个错误奋斗了两天。我尝试过:
- import "multer"
- import { Multer } from "multer"
- import { Express } from "express"
- 在tsconfig中添加类型: ["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.
答案1
得分: 8
我通过将@types/multer
添加到我的依赖项中解决了相同的问题。
所以要么
yarn add @types/multer
或 npm install --save @types/multer
英文:
I solved the same issue by adding @types/multer
to my dependencies.
So either
yarn add @types/multer
or npm install --save @types/multer
答案2
得分: 1
在安装完npm install -D @types/multer
后,在你的tsconfig.json
文件的compilerOptions
中的types
属性下添加"Multer":
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"] // <-- 在这里添加"Multer"
}
}
说明: 在你的tsconfig.json
文件中,可能有一个compilerOptions
下的types
属性。根据这个TypeScript的定义,如果指定了types
,只有列出的包会包含在全局范围内。因此,如果"Multer"没有被列在这里,它就不会自动包含在全局范围内,这就是为什么你会得到一个错误*Namespace 'global.Express' has no exported member 'Multer'.*的原因。
快捷方法:
警告: 这只是一种将Multer引入Express命名空间的方法,你应该确保类型定义对TypeScript可用,就像我上面解释的那样。
import 'multer'; // 一个将Multer引入Express命名空间的方法
// ...
async createNewPost(
// ...
@UploadedFile() file: Express.Multer.File,
// ...
)
附注:在生产环境中,你可能需要将@types/multer
从devDependencies
移动到dependencies
,原因不是很清楚。
附注2:如果你在一个nx.workspace中工作,请确保在Nest(API)文件夹下的tsconfig.app.json中编辑"types"。
tsconfig.app.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["node", "Multer"], // <-- 在这里添加"Multer"
"emitDecoratorMetadata": true,
"target": "es2015"
},
"exclude": ["**/*.spec.ts", "**/*.test.ts"],
"include": ["**/*.ts"]
}
英文:
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"]
}
答案3
得分: 0
如果添加@types/multer
并导入它们没有帮助,尝试在IDE中更改TypeScript版本。
在VSCode命令面板中 -> TypeScript: 选择TypeScript版本 -> 使用工作区版本。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论