【发布时间】:2019-04-08 02:17:07
【问题描述】:
我正在尝试使用 typescript 、 express 构建应用程序。但我收到了这个错误:
Cannot invoke an expression whose type lacks a call signature. Type 'typeof e' has no compatible call signatures(在 app.ts 中调用 express() )
我在这里使用 webpack 来帮助我的开发。
我的 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
};
我的app.ts:
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,但我的构建失败并出现错误。
尝试在一些博客中搜索解决方案,但没有人提到有关此错误的任何信息。我设法在app.ts 中添加express.Application 作为app 的类型
我究竟做错了什么 ?是不是因为webpack的配置?
任何帮助表示赞赏
【问题讨论】:
-
您的依赖项中是否缺少
express? -
@pzaenger 是的,我想我错过了。但它仍然是一样的:(。添加快递后错误仍然存在
-
@ZeroCho 它在我打电话给
express()的同一个地方。我想我解决了。刚刚将导入更改为:import express from "express"。它解决了这个问题。 @pzaenger,感谢您为我指明正确的方向 -
@pzaenger 为什么
import express from "express"有效而import * as express from "express"无效? -
在这个答案中,你可以得到
esModuleInterop是如何导致这个错误的详细解释:stackoverflow.com/questions/56238356/…
标签: node.js typescript express webpack