I have a project with the following dependencies:
"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"
},
my i18n configuration code is as following:
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 };
My app code is as following:
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}`)
);
When I start the application by running npm start, I receive the error: TypeError: Cannot read property ‘use’ of undefined
This error exist because Backend in the configuration file is undefined.
I have used https://www.npmjs.com/package/i18next-http-middleware as a resource.
Anyone have an idea why I’m receiving this error and how to fix this?
Thanks in advanced!!!