[英]how to solve Error : Cannot find module './routes/products' while using router on express js
let express = require('express');
let router = express.Router();
router.use('/', (req,res)=>{
res.send('I am product');
});
module.exports = router;
let express = require('express');
let router = express.Router();
router.use('/', (req,res)=>{
res.send('I am product');
});
module.exports = router;
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);
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:您可以检查以下几项以解决此问题:
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.