Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

319
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!