I just clone my repository on my Ubunto 20.04 I have installed everything but I keep getting this error when I try to run the server. It works well on my windows machine.
Error: Cannot find module '../../Middlewares/AllowCors' Require stack:
- /home/medvik/Documents/github/nextjs-ecom/ecomApi/routes/User/CreateUser.js
- /home/medvik/Documents/github/nextjs-ecom/ecomApi/app.js
- /home/medvik/Documents/github/nextjs-ecom/ecomApi/bin/www at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) 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. (/home/medvik/Documents/github/nextjs-ecom/ecomApi/routes/User/CreateUser.js:4:19) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10) 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) { code: 'MODULE_NOT_FOUND',
The core middleware file path:/MiddleWare/AllowCors.js
const cors = require("cors");
const whitelist = [
"http://localhost:8080",
"http://localhost:3000",
];
var corsOptionsDelegate = (req, callback) => {
var corsOptions;
if (whitelist.indexOf(req.header("Origin")) !== -1) {
corsOptions = { origin: true };
} else {
corsOptions = { origin: false };
}
callback(null, corsOptions);
};
exports.cors = cors();
exports.corsWithOptions = cors(corsOptionsDelegate);
My CreateUser file is in /router/User/CreateUser.js
const express = require("express");
const bcrypt = require("bcryptjs");
const joi = require("joi");
const allowCors = require("../../Middlewares/AllowCors");
router.route("/")
.options(allowCors.corsWithOptions, (req, res) => {
res.sendStatus(200);
}).post(allowCors.corsWithOptions, async (req, res) => {
try {}} catch (err) {
console.log(err);
}
});
The problem is with the file system Linux is is case sensitive unlike windows in my code, I had imported the cors middleware like this
const allowCors = require("../../Middlewares/AllowCors");
this works well in windows even tho the correct import should have been
const allowCors = require("../../MiddleWares/AllowCors");
since I named my middleware folder as MiddleWares.