Home > database >  how to solve Error : Cannot find module './routes/products' while using router on express
how to solve Error : Cannot find module './routes/products' while using router on express

Time:01-11

  • this is products.js code path => (./routes/products.js)
let express = require('express');

let router = express.Router();

router.use('/', (req,res)=>{
    res.send('I am product');
});

module.exports = router;

  • this is users.js code path => (./routes/users.js)
let express = require('express');

let router = express.Router();

router.use('/', (req,res)=>{
    res.send('I am product');
});

module.exports = router;
  • this is index.js code path => (./index.js)
let express = require('express');

let productsRouter = require("./routes/products");
let usersRouter = require("./routes/users");

let app = express();

app.use('/products',productsRouter);
app.use('/users',usersRouter);

app.listen(1212);
  • this is error when i try to start node./index.js how to fix this and little explanation
Error: Cannot find module './routes/products'
Require stack:
- C:\Users\Nile-Tech\3D Objects\project\Express JS\index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (C:\Users\Nile-Tech\3D Objects\project\Express JS\index.js:5:22)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\\Users\\Nile-Tech\\3D Objects\\project\\Express JS\\index.js' ]
}

CodePudding user response:

This error message is indicating that the application is unable to find the './routes/products.js' file.

Based on the error message, it seems that the 'index.js' file and the './routes/products.js' file are located in the following directory: C:\Users\Nile-Tech\3D Objects\project\Express JS

Here are a few things that you can check to resolve this issue:

  1. Make sure that the file 'products.js' is located in a directory called 'routes' and it's located in the same directory as 'index.js'.
  2. Check that the spelling of the file name 'products.js' is correct and it's not typo mistakes.
  3. Check that the casing of the directory and file names are correct, as file and directory names are case-sensitive on some operating systems.
  4. Make sure that you haven't accidentally deleted the products.js file

If none of these suggestions resolve the issue, you can try to run the following command to check your directories, "dir /s | find /i products.js" it will search your PC to find this file.

CodePudding user response:

When you set up "use" as the middleware, in your case express will try to find another module inside your products/users.js, but since it looks like products.js and users.js was your end point you just need to set up the method to use the end point.

(./routes/products.js)

  const express = require('express');
  const router = express.Router();
    
  router.get('/', (req,res)=>{     // if you re using get method
      res.send('I am product');
  });
    
  module.exports = router;

(./routes/users.js)

  const express = require('express');

  const router = express.Router();

  router.get('/', (req,res)=>{    // if you re using get method
      res.send('I am a user');
  });

  module.exports = router;

btw you can replace all your let with the constant const

  • Related