错误:找不到模块'routes'需要堆栈节点.js



每当我尝试运行我的express服务器时,我总是从node.js

获得相同的错误下面是我的代码:
import express, { Application, Request, Response } from "express";
import routes from "routes";
const app: Application = express();
app.use("/api", routes);
const port = 3000;
app.use(express.json());
app.get("/", (req: Request, res: Response) => {
res.send("<h1>Hi api<h1>");
});
app.listen(port, () => {
console.log(`app is running on port ${port}`);
});

这里是我得到的错误:

Error: Cannot find module 'routes'
Require stack:
- E:WorkBedayaapisrcindex.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (E:WorkBedayaapinode_modules@cspotcodesource-map-supportsource-map-support.js:811:30)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (E:WorkBedayaapisrcindex.ts:2:1)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Module.m._compile (E:WorkBedayaapinode_modulests-nodesrcindex.ts:1618:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Object.require.extensions.<computed> [as .ts] (E:WorkBedayaapinode_modulests-nodesrcindex.ts:1621:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'E:\Work\Bedaya\api\src\index.ts' ]

当我尝试将"routes"更改为"./routes"时,它给了我同样的错误,但也有另一个模块。

Error: Cannot find module 'database'
Require stack:
- E:WorkBedayaapisrcmodelsUser.ts
- E:WorkBedayaapisrccontrollersusers.controller.ts
- E:WorkBedayaapisrcroutesapiusers.route.ts
- E:WorkBedayaapisrcroutesindex.ts
- E:WorkBedayaapisrcindex.ts
requireStack: [
'E:\Work\Bedaya\api\src\models\User.ts',
'E:\Work\Bedaya\api\src\controllers\users.controller.ts',
'E:\Work\Bedaya\api\src\routes\api\users.route.ts',
'E:\Work\Bedaya\api\src\routes\index.ts',
'E:\Work\Bedaya\api\src\index.ts'
]

使用import routes from "./routes";,因为它是本地文件。与./database或任何其他本地导入相同。

相关内容

  • 没有找到相关文章

最新更新