我是新学习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”
1条答案
按热度按时间zy1mlcev1#
你不需要index.js和server.js文件,你可以使用index.js进行基本配置。希望这段代码对你有所帮助。