【问题标题】:The requested module does not provide an export named default via express routing请求的模块不通过快速路由提供名为 default 的导出
【发布时间】:2023-02-02 07:39:58
【问题描述】:

我需要帮助来找到我在这里到底缺少什么。

如果有帮助,我的应用程序在 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)

作为一个对这些东西有些陌生的人,当我在我的路由器中专门定义一个默认导出时,我真的不知道出了什么问题。如果有人可以帮助我了解这里到底发生了什么,将不胜感激

【问题讨论】:

  • 你能检查一下./assets/js/router.mjs模块导出了什么吗?

标签: javascript node.js express


【解决方案1】:

我想你忘了保存 router.mjs 文件,因为我犯了同样的错误并得到了这个错误:)

【讨论】:

  • 您的答案可以通过其他支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写出好的答案的信息in the help center
【解决方案2】:

试试这个代码

const express = require('快递');

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 2021-11-02
    • 2022-01-25
    • 2022-11-16
    • 2021-10-03
    • 1970-01-01
    • 2019-10-06
    • 2019-08-31
    相关资源
    最近更新 更多