![](/img/trans.png)
[英]Express: The requested module does not provide an export named 'User'
[英]The requested module does not provide an export named default via express routing
我需要帮助来找到我在这里到底缺少什么。
如果有帮助,我的应用程序位于 node.js (16.13.1),使用最新版本的 express 以及 nginx 反向代理。
应用程序.mjs
import router from './assets/js/router.mjs'; //Imports our routes
import express from 'express';
const expressapp = express(); //Creates the application with express
const moduleURL = new URL(import.meta.url); //Creates a new URL to use for the current directory name
const __dirname = path.dirname(moduleURL.pathname); //Creates the directory name and attaches it to the __dirname constant.
//Middleware
//Instructions for the app to use body parser & JSON goes here
expressapp.use('/subscribe', router); //Use the subscribe route
expressapp.use(express.static(__dirname, {index: 'index.html'})); //Tells the app to use the current path, with the index being index.html
expressapp.post('/subscribe', (req, res) =>
{
//post request stuff goes here
}
路由器.mjs
import express from 'express';
var router = express.Router();
// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', function (req, res) {
res.send('Gaming Galaxy Landing Page')
})
// define the subscribe route
router.get('/subscribe', function (req, res) {
res.send('Subscribe route')
})
export default router;
错误
import router from './assets/js/router.mjs'; //Imports our routes
^^^^^^
SyntaxError: The requested module './assets/js/router.mjs' does not provide an export named 'default'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
作为一个对这些东西有些陌生的人,当我在我的路由器中专门定义一个默认导出时,我真的不知道出了什么问题。 如果有人可以帮助我了解这里到底发生了什么,将不胜感激
我想你忘了保存 router.mjs 文件,因为我犯了同样的错误并得到了这个错误:)
试试这个代码
const express = require('快递');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.