当我运行一些代码来寻找来自快速服务器的请求和响应时,我遇到了一个问题,它无法找到模块“快速”的声明。这个错误从
Could not find a declaration file for module 'express'.
到SyntaxError: Cannot use import statement outside a module
我尝试使用
const express = require('express')
。接下来我尝试的是通过 npm 安装类型。我想知道我是否必须为我的服务器创建自己的类型定义。
import express, {request, response} from 'express';
const dictionary = {
"apple": "A round fruit with red or green skin and a white flesh.",
"banana": "A long curved fruit which grows in clusters and has soft pulpy flesh.",
"orange": "A spherical fruit with a tough bright reddish-yellow rind.",
};
const app = express();
app.use(express.json());
// Define an API endpoint to get a definition for a word
app.get('/:dictionary', function (req: request, res: response) {
console.log(req.params[dictionary]);
res.send();
});
app.listen(3000, () => {
console.log("Server listening on port 3000");
});
这是我的代码。
看起来你正在使用 es6 import 语句来导入 express 但你没有使用模块系统。要在 tsconfig.json 文件中解决这个问题,请更改模块值
commonjs
.
tsconfig.json
"module": "commonjs",
"esModuleInterop": true
然后安装类型
npm install @types/node @types/express --dev