这是我的
app.ts
文件:
import express, { Request, Response } from "express";
import dotenv from "dotenv";
dotenv.config();
import config from "config";
import responseTime from "response-time";
import connect from "./utils/connect";
import logger from "./utils/logger";
import routes from "./routes";
import deserializeUser from "./middleware/deserializeUser";
import { restResponseTimeHistogram, startMetricsServer } from "./utils/metrics";
import swaggerDocs from "./utils/swagger";
const port = config.get<number>("port");
const app = express();
app.use(express.json());
app.use(deserializeUser);
app.use(
responseTime((req: Request, res: Response, time: number) => {
if (req?.route?.path) {
restResponseTimeHistogram.observe(
{
method: req.method,
route: req.route.path,
status_code: res.statusCode,
},
time * 1000
);
}
})
);
app.listen(port, async () => {
logger.info(`App is running at http://localhost:${port}`);
await connect();
routes(app);
startMetricsServer();
swaggerDocs(app, port);
});
我确实收到此错误消息:
yarn dev
yarn run v1.22.17
warning package.json: License should be a valid SPDX license expression
warning ..\..\..\..\package.json: No license field
$ ts-node-dev --respawn --transpile-only src/app.ts
[INFO] 14:02:52 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.6.3)
TypeError: Cannot read properties of undefined (reading 'config')
at Object.<anonymous> (C:\Users\A\Desktop\desktop\base_code\back_bc_node\src\app.ts:3:8)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Module._compile (C:\Users\A\Desktop\desktop\base_code\back_bc_node\node_modules\source-map-support\source-map-support.js:568:25)
at Module.m._compile (C:\Users\A\AppData\Local\Temp\ts-node-dev-hook-16796532812275466.js:69:33)
at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at require.extensions.<computed> (C:\Users\A\AppData\Local\Temp\ts-node-dev-hook-16796532812275466.js:71:20)
at Object.nodeDevHook [as .ts] (C:\Users\A\Desktop\desktop\base_code\back_bc_node\node_modules\ts-node-dev\lib\hook.js:63:13)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
[ERROR] 14:02:52 TypeError: Cannot read properties of undefined (reading 'config')
为什么我会收到此错误消息?我已经使用
dotenv version 10
将此代码与我以前的应用程序一起使用,但是当我使用 dotenv version 16
将它复制到我的新应用程序中时,我收到此错误消息。
这是我的
package.json
文件:
{
"name": "back_bc_node",
"version": "1.0.0",
"main": "index.js",
"license": "Commercial",
"scripts": {
"dev": "ts-node-dev --respawn --transpile-only src/app.ts",
"build": "tsc"
},
"dependencies": {
"bcrypt": "^5.0.1",
"config": "^3.3.7",
"cors": "^2.8.5",
"dayjs": "^1.11.0",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"mongoose": "^6.2.10",
"nanoid": "^3.3.2",
"pino": "^7.10.0",
"pino-pretty": "^7.6.1",
"prom-client": "^14.0.1",
"response-time": "^2.3.2",
"swagger-jsdoc": "^6.2.0",
"swagger-ui-express": "^4.3.0",
"zod": "^3.14.4"
},
"devDependencies": {
"@types/bcrypt": "^5.0.0",
"@types/body-parser": "^1.19.2",
"@types/config": "^0.0.41",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.8",
"@types/lodash": "^4.14.181",
"@types/nanoid": "^3.0.0",
"@types/node": "^17.0.23",
"@types/pino": "^7.0.5",
"@types/response-time": "^2.3.5",
"ts-node-dev": "^1.1.8",
"typescript": "^4.6.3"
}
}
尝试替换:
import dotenv from "dotenv";
为此:
import * as dotenv from 'dotenv'
我也遇到了这个问题,我通过以下方式解决了它:
新建一个项目,将旧项目中的重要文件(对我来说,是Vue项目中的所有src文件),然后启动项目,成功!
我有同样的问题。我必须将
"esModuleInterop": true,
添加到 tsconfig.json,例如
{
"compilerOptions": {
"esModuleInterop": true,
"target": "es5",
"lib": ["es5", "dom"],
"types": ["@testing-library/cypress", "cypress", "node"]
},
"include": ["../cypress.config.ts", "**/*.ts"]
}
这是因为你试图在 es 模块中使用 commonJS 的语法。 dotenv.config 函数用于 commonJS 并使用 TypeScript ES 模块。为了更好地使用,请在此处查看 dotenv 的文档: