我有一个项目与以下依赖关系:
"dependencies": {
"@hapi/joi": "^17.1.1",
"bcrypt": "^5.0.1",
"config": "^3.3.7",
"express": "^4.17.3",
"express-async-errors": "^3.1.1",
"express-rest-i18n": "^1.0.1",
"http-status-codes": "^2.2.0",
"i18next": "^21.6.14",
"i18next-http-backend": "^1.4.0",
"i18next-http-middleware": "^3.2.0",
"joi": "^17.4.2",
"lodash": "^4.17.21",
"mongoose": "^6.2.4",
"validator": "^13.7.0"
},
字符串
我的i18n配置代码如下:
const i18next = require("i18next").default;
const Backend = require("i18next-http-backend").default;
const i18nextMiddleware = require("i18next-http-middleware");
i18next
.use(Backend)
.use(i18nextMiddleware.LanguageDetector)
.init({
backend: {
loadPath: __dirname + `/locales/{{lng}}/{{ns}}.json`,
},
fallbackLng: "en",
preload: ["en"],
});
module.exports = { i18next };
型
我的app代码如下:
const { i18next } = require("../locales/i18n");
const messages = require("../locales/en/translation.json");
const i18nextMiddleware = require("i18next-http-middleware");
require("express-async-errors");
const express = require("express");
const app = express();
app.use(express.json());
app.use(i18nextMiddleware.handle(i18next));
app.get("*", (req, res) => {
const response = req.t("greeting");
res.status(200);
res.send(response);
});
const port = process.env.PORT || 3000;
module.exports = app.listen(port, () =>
console.log(`Listening on port: ${port}`)
);
型
当我通过运行npm start启动应用程序时,我收到错误:TypeError:Cannot read property 'use' of undefined
此错误存在是因为配置文件中的Backend未定义。我使用https://www.npmjs.com/package/i18next-http-middleware作为资源。
为什么我会收到这个错误,我该如何解决这个问题?
2条答案
按热度按时间jljoyd4f1#
为什么要访问.default?
像这样导入:
字符串
6ie5vjzr2#
阅读:https://www.i18next.com/overview/typescript
在您的
tsconfig.json
中,请确保添加:字符串