我正在尝试导出一个包含Mongo URL密钥的Es6模块(我正在使用Es6实验性标志),但似乎我做的不正确。因为出现此错误:
internal/modules/esm/resolve.js:58
let url = moduleWrapResolve(specifier, parentURL);
Error: Cannot find module C:\Users\Desktop\server.mjs
←[90m at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:58:13)←[39m
←[90m at Loader.resolve (internal/modules/esm/loader.js:85:40)←[39m
←[90m at Loader.getModuleJob (internal/modules/esm/loader.js:188:40)←[39m
←[90m at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)←[39m
←[90m at link (internal/modules/esm/module_job.js:41:36)←[39m {
code: ←[32m'ERR_MODULE_NOT_FOUND'←[39m
这是MongoURI:
//config/index.mjs
export const mongoURI ='mongodb://localhost:27017/MERN',
...和入口点
//server.mjs
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
const app = express();
//body parser middleware
app.use(bodyParser.json());
//DB configuration
import mongoURI from './config';
mongoose
.connect(mongoURI)
.then(()=>console.log('MongoDB connected...'))
.catch(err=>console.log(err));
const port = process.env.PORT||5000
app.listen(port,()=>console.log(`Server started on port ${port}`))