Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Express says Router.use() requires a middleware function but got an Object

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

Short answer

app.use() or router.use() was given something that is not a function, here a plain object. A route file exports an object instead of the router, or require() returned an ES module namespace, or a circular require handed back a half-built exports object. Log typeof on what you mount to find which.

  • Applies to Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the type of every router you mount. The one that prints object instead of function is the file to fix.

    const users = require('./routes/users');
    console.log(typeof users, Object.keys(users));
    app.use('/users', users);
  2. 2Match what you see, then jump to its fix

    1. 1typeof prints object and Object.keys shows a name such as router, or nothingThe route file exports an object, or never assigns module.exportsFix
    2. 2Object.keys prints [ 'default' ]A route file uses export default but is loaded with requireFix
    3. 3typeof prints object for a file that does export a router, and only at start-upTwo files require each other and one gets an unfinished exportFix

What does the error look like?

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

Cause 1 of 3

The route file exports an object, or never assigns module.exports

1Check

In CommonJS only the value of module.exports is returned by require. Writing module.exports = { router } exports an object that holds the router, and assigning to the bare exports variable does not change what is exported.

grep -n "exports" routes/users.js

2Fix

Export the router itself, as the Express routing guide does.

// routes/users.js
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => res.json([]));
module.exports = router;

3Verify

typeof now prints function, and the route answers.

curl -i http://localhost:3000/users

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 3

A route file uses export default but is loaded with require

1Check

When require() loads an ES module, the result is the module namespace object and the default export sits on its .default property.

head -n 5 routes/users.js && grep -n '"type"' package.json

2Fix

Use one module style for the whole project. In an ES module project, import the router; if you must keep require, read .default.

// ES modules
import users from './routes/users.js';
app.use('/users', users);

// or, with require
app.use('/users', require('./routes/users.js').default);

3Verify

Start the app and confirm it no longer throws.

node server.js

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

Two files require each other and one gets an unfinished export

1Check

With circular require calls, Node returns an unfinished copy of the exports object to the file that asked second, so the router is not there yet.

grep -rn "require(" routes/ app.js

2Fix

Break the loop: move the shared code (a helper, a model, the app instance) into its own file that does not require the routes back.

// lib/shared.js has no require of routes/*
const { sharedHelper } = require('../lib/shared');

3Verify

Restart and print typeof for every mounted router.

node server.js

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Keep one export per route file, always the router, and use one module system (CommonJS or ES modules) in the whole project.
  • Name files by what they hold (users.routes.js) and never put app.use calls inside route files that others require back.
  • Keep shared helpers in a folder that route files import but that never imports routes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.