错误节点:内部/错误:491错误捕获堆栈跟踪(错误);在NODEMON SERVER命令中

mm9b1k5b  于 2023-02-03  发布在  Node.js
关注(0)|答案(1)|浏览(333)

我是新学习MERN堆栈的服务器端,我找不到这个问题的任何答案,有人能帮助我解决这个错误吗?我已经尝试了所有可能的解决方案,但它仍然不会工作帮助!

node:internal/errors:491
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\charl\Desktop\movie-reviews\server.js' imported from C:\Users\charl\Desktop\movie-reviews\backend\index.js
at new NodeError (node:internal/errors:400:5)
at finalizeResolution (node:internal/modules/esm/resolve:326:11)
at moduleResolve (node:internal/modules/esm/resolve:945:10)
at defaultResolve (node:internal/modules/esm/resolve:1153:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:842:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
at link (node:internal/modules/esm/module_job:76:36) {
code: 'ERR_MODULE_NOT_FOUND'

}
索引. js

import app from '../server.js'
import mongodb from "mongodb"
import dotenv from "dotenv"

async function main(){

    dotenv.config()
    const client = new mongodb.MongoClient(process.env.MOVIEREVIEWS_DB_URI)

    const port = process.env.PORT || 8000

    try{
        //connect to the mongodb cluster

        await client.connect()
        app.listen(port,()=>{
            console.log("server is running on port: " + port);
        })
    } catch (e){
        console.error(e);
        process.exit(1)
    }
}

main().catch(console.error);

服务器. js

import express from 'express'
import cors from 'cors'
import movies from './api/movies.rout.js'
const app = express()

app.use(cors())
app.use(express.json())
app.use("/api/v1/movies",movies)
app.use('*', (req,res)=>{
    res.status(404).json({error:"not found"})
})

export default app

电影路线. js

import express from 'express'

const router = express.Router() //get access to express router
router.route('/').get((req,res) => res.send('hello world'))

export default router

连接环境

MOVIEREVIEWS_DB_URI=database
MOVIEREVIEWS=sample_mflix
PORT=500

我尝试将其添加到package.json中
“start”:“节点恶魔--实验模块--es模块指定符解析=节点索引. js”

zy1mlcev

zy1mlcev1#

    • 索引. js**
import express from "express"
import dotenv from "dotenv"
import cors from "cors"

// use mongoose package instead of mongodb
import mongoose from "mongoose"

// routes
// should name files in camel case like movieRoutes.js and also use file extension .js
import movies from "./api/moviesRoutes.js"

// configurations
dotenv.config();

const app = express();

app.use(cors());
app.use(express.json());
// middleware for x-www-form-urlencoded
app.use(express.urlencoded({ extended: true}));

// use movie route
app.use("/api/v1/movies", movies);

// MongoDB Connection
const port = process.env.PORT || 8000
mongoose.set("strictQuery", true);
mongoose.connect(process.env.MOVIEREVIEWS_DB_URI).then(() => {
    app.listen(port ,()=> console.log(`server is running on port: ${port}`));
}).
catch((err) => console.log(err));
    • 影片路由. js**
import express from "express"

const router = express.Router();

router.route("/").get((req, res) => {
    res.send("Hello World");
});

export default router;

你不需要index.js和server.js文件,你可以使用index.js进行基本配置。希望这段代码对你有所帮助。

相关问题