Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

321
Vistas
Express router : Router.use() requires a middleware function but got a Object

I know there are lot of questions in SO but none of them gave me a solution

routes/authentication/index.js

import { Router } from 'express'

const router = Router();

router.get('/', (_req, _res) => console.log("Works"))

// module.exports = router                    <-- this works
export default router                      // <-- this doesn't

constants.js

const ROUTES = {
    'AUTHENTICATION' : require('../routes/authentication')
}

export default ROUTES

and using it in app.js as

import express from 'express'
import connectDatabase from './connectDb';
import ROUTES from './constants';
const app = express();

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config();
}
connectDatabase()

app.get('/', (_req, res) => {
  res.send("hello world")
})

app.use('/auth', ROUTES.AUTHENTICATION)

export default app;

Now with module.exports = router works but export default router throws an error

TypeError: Router.use() requires a middleware function but got a Object

I tried finding the cause for the problem but couldn't. Thanks in advance

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You require the router file in constants.js, hence module.exports works.

For export default router to work, you need to import it.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda