node.js - Express 无法在 NodeJS 应用程序中使用 Typescript
问题描述
我正在学习 Typescript,我已经按照教程使用它并逐字表达 api 应用程序,但我收到以下错误:
D:\Development\Node\Ed\TypeScript\src\app.ts:5
const app: Application = express();
^
TypeError: express_1.default is not a function
at Object.<anonymous> (D:\Development\Node\Ed\TypeScript\src\app.ts:5:33)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.m._compile (D:\Development\Node\Ed\TypeScript\node_modules\ts-node\src\index.ts:814:23)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.require.extensions.(anonymous function) [as .ts] (D:\Development\Node\Ed\TypeScript\node_modules\ts-node\src\index.ts:817:12)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at main (D:\Development\Node\Ed\TypeScript\node_modules\ts-node\src\bin.ts:226:14)
我不知道如何解决,并希望得到任何指示
app.ts 文件:
import express, { Application, Request, Response, NextFunction } from 'express';
const app: Application = express();
app.get('/', (req: Request, res: Response, next: NextFunction) => {
res.send('hello');
});
app.listen(5000, () => console.log('Server running'));
tsconfig.json:
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"removeComments": true, /* Do not emit comments to output. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
}
}
解决方案
您需要更改导入文件的方式。它应该是:
import * as express from "express";
import { Application, Request, Response, NextFunction } from 'express';
推荐阅读
- flutter - Flutter - Flutter 的语音增强包
- python - 连接两个 DF,不包括不在一个 df 中的行
- python-3.x - 从纳斯达克下载 csv - 下载按钮上没有链接
- regex - 如何使用正则表达式在 code_ 之后删除 40 个字符?
- html - HTML 中的背景颜色不适用于整个页面
- listview - 获取 Svelte 插槽中每个组件的索引
- python - 获取:“错误:使用 docker 容器化烧瓶应用程序时无法为 xmlsec 构建轮子”
- c# - 如何修复“事件必须是委托类型”错误?(接口内的事件数组)C#
- thingsboard - 如何更改 Thingsboard 规则引擎中的 deviceName 和 deviceType?
- laravel-8 - 如何在刀片/error.php 文件中使用控制器?