Express says Router.use() requires a middleware function but got an Object
TypeError: Router.use() requires a middleware function but got a ObjectShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
typeof prints object and Object.keys shows a name such as router, or nothingThe route file exports an object, or never assigns module.exportsFix - 2
Object.keys prints [ 'default' ]A route file uses export default but is loaded with requireFix - 3
typeof 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
- 1
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.js2Fix
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/usersConfirmed by expressjs.com
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.json2Fix
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.jsConfirmed by nodejs.org
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.js2Fix
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.jsConfirmed by nodejs.org
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
Back end
Express throws 'Route.get() requires a callback function but got a [object Undefined]'
Error: Route.post() requires a callback function but got a [object Object]
Express checks every handler you pass to a route and throws when one is not a function.
3 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fix