How to fix 'MODULE_NOT_FOUND' error in node js when using require() for modules?

Viewed 254

In server.js I am trying to include 2 modules by using require() for routing from the routes folder and passing the app in it. server.js :

const express = require("express");
const cors = require("cors");
const cookieSession = require("cookie-session");
const app = express();

app.use(cors());
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
app.use(
  cookieSession({
    name: "kth-session",
    secret: "COOKIE_SECRET", // should use as secret environment variable
    httpOnly: true,
  })
);
const db = require("../app/models");
const Role = db.role;
db.sequelize.sync({ force: true }).then(() => {
  console.log("Drop and Resync Db");
  initial();
});

function initial() {
  Role.create({
    id: 1,
    name: "user",
  });

  Role.create({
    id: 2,
    name: "moderator",
  });

  Role.create({
    id: 3,
    name: "admin",
  });
}

// simple route
app.get("/", (req, res) => {
  res.json({ message: "Welcome to xss_bank application." });
});
// routes

require("./server/app/routes/user.routes")(app);
require("./server/app/routes/auth.routes")(app);

// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

But when I try to run the server(node server.js) this error occurs: 'MODULE_NOT_FOUND'

PS C:\Users\DELL\xss_fullstak\server\app> node server.js
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module './server/app/routes/user.routes'
Require stack:
- C:\Users\DELL\xss_fullstak\server\app\server.js
    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:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\DELL\xss_fullstak\server\app\server.js:48:1) 
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\\Users\\DELL\\xss_fullstak\\server\\app\\server.js' ]
}

Here you can see the structure of my node server:

And here you can see the modules in the routes folder that I already exported.

'user.routes.js':

const { authJwt } = require("../middleware");
const controller = require("../controllers/user.controller");
module.exports = (app) => {
  app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept");
    next();
  });
  app.get("/api/test/all", controller.allAccess);
  app.get("/api/test/user", [authJwt.verifyToken], controller.userBoard);
  app.get(
    "/api/test/mod",
    [authJwt.verifyToken, authJwt.isModerator],
    controller.moderatorBoard
  );
  app.get(
    "/api/test/admin",
    [authJwt.verifyToken, authJwt.isAdmin],
    controller.adminBoard
  );
};

'auth.routes.js'

const { verifySignUp } = require("../middleware");
const controller = require("../controllers/auth.controller");
module.exports = (app) => {
  app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept");
    next();
  });
  app.post(
    "/api/auth/signup",
    [
      verifySignUp.checkDuplicateUsernameOrEmail,
      verifySignUp.checkRolesExisted,
    ],
    controller.signup
  );
  app.post("/api/auth/signin", controller.signin);
  app.post("/api/auth/signout", controller.signout);
};

The right paths for those modules are "./server/app/routes/user.routes" and "./server/app/routes/auth.routes", but I have already tried a different path whit (.js) and (../) and more, but still doesn't work !!! How can I solve it?

'package.json':

{
  "name": "server",
  "version": "1.0.0",
  "description": "node.js, express, login, registration, rest api, jwt, authentication, authoriz",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "KTH",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "cookie-session": "^2.0.0",
    "cors": "^2.8.5",
    "express": "^4.17.3",
    "jsonwebtoken": "^8.5.1",
    "mysql": "^2.18.1",
    "mysql2": "^2.3.3",
    "sequelize": "^6.18.0"
  }
}
0 Answers
Related