简体   繁体   English

如何解决错误:在 express js 上使用路由器时找不到模块“./routes/products”

[英]how to solve Error : Cannot find module './routes/products' while using router on express js

  • this is products.js code path => (./routes/products.js)这是 products.js 代码路径 => (./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)这是 users.js 代码路径 => (./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)这是 index.js 代码路径 => (./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这是我尝试启动 node./index.js 时的错误 如何解决这个问题,解释不多
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' ]
}

This error message is indicating that the application is unable to find the './routes/products.js' file.此错误消息表明应用程序无法找到“./routes/products.js”文件。

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根据错误消息,“index.js”文件和“./routes/products.js”文件似乎位于以下目录中: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'.确保文件“products.js”位于名为“routes”的目录中,并且与“index.js”位于同一目录中。
  2. Check that the spelling of the file name 'products.js' is correct and it's not typo mistakes.检查文件名“products.js”的拼写是否正确,并且不是拼写错误。
  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确保您没有意外删除 products.js 文件

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.如果这些建议都不能解决问题,您可以尝试运行以下命令来检查您的目录,“dir /s | find /i products.js”它将搜索您的 PC 以找到此文件。

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.当您将“使用”设置为中间件时,在您的情况下,express 将尝试在您的 products/users.js 中找到另一个模块,但由于它看起来像 products.js 和 users.js 是您的终点,您只需要设置向上使用终点的方法。

(./routes/products.js) (./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) (./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顺便说一句,你可以用常量const替换你所有的let

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM