Node.js 打字脚本+;Express:Type';e'的类型;没有兼容的呼叫签名
我正在尝试使用typescript,express构建一个应用程序。但我得到了一个错误:Node.js 打字脚本+;Express:Type';e'的类型;没有兼容的呼叫签名,node.js,typescript,express,webpack,Node.js,Typescript,Express,Webpack,我正在尝试使用typescript,express构建一个应用程序。但我得到了一个错误: 无法调用其类型缺少调用签名的表达式。类型“typeof e”没有兼容的调用签名(在app.ts中调用express()) 我在这里使用webpack来帮助我的发展 My Package.json: "scripts" :{ "build": "webpack" }, "dependencies": { "body-parser": "^1.18.3", "dotenv": "^
无法调用其类型缺少调用签名的表达式。类型“typeof e”没有兼容的调用签名
(在app.ts中调用express())
我在这里使用webpack来帮助我的发展
My Package.json:
"scripts" :{
"build": "webpack"
},
"dependencies": {
"body-parser": "^1.18.3",
"dotenv": "^6.1.0",
"jsonwebtoken": "^8.3.0",
"nodemon": "^1.18.5"
},
"devDependencies": {
"@types/body-parser": "^1.17.0",
"@types/dotenv": "^4.0.3",
"@types/express": "^4.16.0",
"clean-webpack-plugin": "^0.1.19",
"ts-loader": "^5.3.0",
"ts-node": "^7.0.1",
"typescript": "^3.1.6",
"webpack": "^4.24.0",
"webpack-cli": "^3.1.2"
}
我的webpack.confg.js
:
var path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin");
var fs = require("fs");
var nodeModules = {};
fs.readdirSync("node_modules")
.filter(function(x) {
return [".bin"].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = "commonjs " + mod;
});
module.exports = {
entry: "./src/index.ts",
plugins: [new CleanWebpackPlugin(["./dist"])],
output: {
filename: "index.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
//all files with .ts extention will be handled y ts-loader
{ test: /\.ts$/, loader: "ts-loader" }
]
},
target: "node",
externals: nodeModules
};
import * as express from "express";
import * as bodyParser from "body-parser";
class App {
public app: express.Application;
constructor() {
this.app = express();
this.config();
}
private config(): void {
//add support for application/json type for data
this.app.use(bodyParser.json());
//support application/x-www-form-urlencoded post data
this.app.use(bodyParser.urlencoded({ extended: false }));
}
}
export default new App().app;
我的app.ts
:
var path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin");
var fs = require("fs");
var nodeModules = {};
fs.readdirSync("node_modules")
.filter(function(x) {
return [".bin"].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = "commonjs " + mod;
});
module.exports = {
entry: "./src/index.ts",
plugins: [new CleanWebpackPlugin(["./dist"])],
output: {
filename: "index.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
//all files with .ts extention will be handled y ts-loader
{ test: /\.ts$/, loader: "ts-loader" }
]
},
target: "node",
externals: nodeModules
};
import * as express from "express";
import * as bodyParser from "body-parser";
class App {
public app: express.Application;
constructor() {
this.app = express();
this.config();
}
private config(): void {
//add support for application/json type for data
this.app.use(bodyParser.json());
//support application/x-www-form-urlencoded post data
this.app.use(bodyParser.urlencoded({ extended: false }));
}
}
export default new App().app;
我正在运行npm run build
,我的构建失败,并显示错误。
尝试在一些博客中搜索解决方案,但没有人提到有关此错误的任何内容。我设法将express.Application
作为app
的类型添加到app.ts
我做错了什么?是因为网页包的配置吗
任何帮助都需要从express导入默认导出,而不是名称空间(名称空间是包含所有命名导出的对象) 在您的
app.ts
中,这应该是您所需要的全部:
// Change these
import express from "express";
import bodyParser from "body-parser";
区别在于:
// Namespace import
import * as express from "express";
const app = express.default();
// Default import
import express from "express";
const app = express();
你的相依关系中缺少
express
吗?@pzaenger是的,我想我错过了。但仍然是一样的:(.添加后错误仍然存在express@ZeroCho它就在我调用express()
的同一个地方。我想我解决了它。只是将导入更改为:import express from“express”
。它解决了这个问题。@pzaenger,谢谢你给我指出正确的方向direction@pzaenger为什么从“express”
导入express有效,而从“express”导入的导入*as express无效?在这个答案中,您可以详细解释esModuleInterop
如何导致此错误: