Express throws 'Route.get() requires a callback function but got a [object Undefined]'
Error: Route.get() requires a callback function but got a [object Undefined]Short answer
Express checks every handler you pass to a route and throws when one is not a function. 'object Undefined' means the name you passed has no value: the module exports it differently, the require loop ran too early, or the import style does not match the export. Log the value to see which.
Fix it fast
1Find out which cause you have
The stack trace names the line with the route. Print the typeof of every handler on that line before the route is registered.
console.log(typeof userController.getUser, typeof auth); // 'function' is good; 'undefined' or 'object' is the bug2Match what you see, then jump to its fix
- 1
typeof is 'object' (an empty {}) for the whole imported module, or the function is missing from itThe module reassigns exports instead of module.exportsFix - 2
The handler is undefined only on startup; the same file works when run on its own. Two files require each other, directly or through a thirdA circular require gave a half-finished moduleFix - 3
Works with a default import and breaks with braces, or the reverse. The export is a single function but you destructure itThe import style does not match the export (default vs named)Fix
- 1
What does the error look like?
Error: Route.get() requires a callback function but got a [object Undefined]Error: Route.post() requires a callback function but got a [object Object]
Cause 1 of 3
The module reassigns exports instead of module.exports
1Check
In Node.js, exports is only a shortcut to module.exports. Assigning a new value to exports cuts the link, so require() still returns the original empty object.
node -e "console.log(require('./controllers/user'))"2Fix
Assign to module.exports, or attach properties to exports without replacing it.
// controllers/user.js
// wrong: exports = { getUser };
module.exports = { getUser };3Verify
The same one-liner now prints the functions.
node -e "console.log(typeof require('./controllers/user').getUser)"Confirmed by nodejs.org
Cause 2 of 3
A circular require gave a half-finished module
1Check
Node.js docs state that with circular require() calls a module may be returned before it has finished running, and the requiring file gets an unfinished copy of its exports.
grep -rn "require('\.\./routes\|require('\./app')" src2Fix
Break the loop: move the shared piece into a third file that requires nothing from the two, or require the module inside the function that uses it.
// controllers/user.js: do not require the app here
const { getUser } = require('../services/user'); // services never require controllers3Verify
Start the app; the route registers without the error.
node src/server.jsConfirmed by nodejs.org
Cause 3 of 3
The import style does not match the export (default vs named)
1Check
When ESM code imports a CommonJS file, the default import is module.exports, and named exports are found only by static analysis. If a file does module.exports = fn, then const { fn } = require(...) is undefined.
// what does the file really export?
node --input-type=module -e "import * as m from './middleware/auth.js'; console.log(Object.keys(m))"2Fix
Import the way the file exports: use the default import for module.exports = fn, and braces only for named exports. A misspelled property name gives undefined in the same way.
// auth.js: module.exports = function auth(req, res, next) { next(); };
const auth = require('./middleware/auth'); // right
// const { auth } = require('./middleware/auth'); // undefined
router.get('/me', auth, getMe);3Verify
Log typeof auth; it must be 'function'.
node -e "console.log(typeof require('./middleware/auth'))"Confirmed by nodejs.org
How do I stop it coming back?
- Pass the function itself to the route, never its result: write router.get('/', handler), not handler().
- Use one module style per project and export controllers as one object so the import shape is obvious.
- Keep route files free of imports that point back at the app file.
- Add one smoke test that imports the app so a bad export fails in CI, not on deploy.
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 answers Cannot GET for a page that should exist
Cannot GET /api/users
Cannot GET is Express's default reply when no route or static file matches the request.
4 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