Maaz Zindani

Loading the index…

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

Express throws 'Route.get() requires a callback function but got a [object Undefined]'

ErrorError: 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.

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

Fix it fast

  1. 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 bug
  2. 2Match what you see, then jump to its fix

    1. 1typeof is 'object' (an empty {}) for the whole imported module, or the function is missing from itThe module reassigns exports instead of module.exportsFix
    2. 2The 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. 3Works 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

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

Did this fix it?

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')" src

2Fix

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 controllers

3Verify

Start the app; the route registers without the error.

node src/server.js

Confirmed by nodejs.org

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

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