Namespace ‘global.Express’ has no exported member ‘Multer’.

huangapple go评论68阅读模式
英文:

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.

Namespace ‘global.Express’ has no exported member ‘Multer’.

答案1

得分: 8

我通过将@types/multer添加到我的依赖项中解决了相同的问题。

所以要么
yarn add @types/multernpm 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/multerdevDependencies移动到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

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

Explanation: In your tsconfig.json file, possibly there's a &quot;types&quot; 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 &#39;multer&#39;; // 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

{
	&quot;extends&quot;: &quot;./tsconfig.json&quot;,
	&quot;compilerOptions&quot;: {
		&quot;outDir&quot;: &quot;../../dist/out-tsc&quot;,
		&quot;module&quot;: &quot;commonjs&quot;,
		&quot;types&quot;: [&quot;node&quot;, &quot;Multer&quot;],    // &lt;-- Add &quot;Multer&quot; here
		&quot;emitDecoratorMetadata&quot;: true,
		&quot;target&quot;: &quot;es2015&quot;
	},
	&quot;exclude&quot;: [&quot;**/*.spec.ts&quot;, &quot;**/*.test.ts&quot;],
	&quot;include&quot;: [&quot;**/*.ts&quot;]
}

答案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.

huangapple
  • 本文由 发表于 2023年3月10日 01:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定