Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Cannot find module express cors

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);
  }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!