简体   繁体   中英

The requested module does not provide an export named default via express routing

I'm in the need of assistance on finding what exactly i'm missing here.

If it helps, my app is on node.js (16.13.1) using the latest version of express as well as a nginx reverse proxy.

app.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
}

router.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;

Error

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)

As someone who's somewhat new to this stuff, I really don't know what's wrong when I specifically defined a default export in my router. If anyone could help me learn what exactly is going on here it'll be very much appreciated

I think u have forgotten to save router.mjs file as I did the same mistake and got this error:)

try this code

const express = require('express');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM