• Register
0 votes

Problem :

Facing following express issue
throw new typeerror('router.use() requires a middleware function but got a ' + gettype(fn))
6 5 3
6,930 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

I had faced same issue in recent past.

After doing the research i found following solution to your issue:

If your are using express and your code is as below

const router = express.Router();

Then please make sure to update your code as below

module.exports = router;

And please do it at the end of your page

9 7 4
38,600 points
0 votes

This error occurs when your route modules are not being exported or you forget to export the modules which use the router. Means Express does not have access to it when tries to identify all of your routes.


Solution 1:

You can fix it by adding a module.exports = router; at the end of each your route files.


var express = require('express');

var router = express.Router();

router.get('/', function(req, res, next) {

     //Do whatever...


module.exports = router;

Solution 2:

If your app.js is the main starting point of the app, then it should have;

const port = process.env.PORT || 3000;

app.listen(port, () => console.log(`Listening on port ${port}...`));

instead of ;

modules exports = app;

Generally, index.js is used for the starting point of the app. Rename index.js as helloworld.js and change the same at the require statements;

var routes = require('./routes/index');


var routes = require('./routes/helloworld');

After doing all the changes run the following command

node app.js

Solution 3:

Another method of having this error is to call a method that does not exist or not exported. If you are calling the login method but forget to define them.


For example, you are calling the method

app.post( ‘/api/login’, db.login);


11 5 2
3,890 points

Related questions

0 votes
1 answer 22 views
Problem : Facing following express issue throw new typeerror('router.use() requires a middleware function but got a ' + gettype(fn)) any help?!
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 147 views
0 votes
1 answer 20 views
Problem: throw new typeerror('router.use() requires middleware function but got a ' + gettype(fn));
asked Feb 23 Muneeb Saadii 125k points
0 votes
0 answers 58 views
TypeError: Router.use() requires a middleware function but got a Object at Function.use (E:\firebase-CRUD_api\functions\node_modules\express\lib\router\index.js:458:13) at Function.<anonymous> (E:\firebase-CRUD_api\functions\node_modules\express\lib\application.js:220:21) ... at Function.Module._load (internal/modules/cjs/loader.js:899:14) at Module.require (internal/modules/cjs/loader.js:1042:19)
asked May 17, 2020 anonymous
0 votes
1 answer 24 views
Problem: HELP me to deal with this : Error: route.get() requires a callback function but got a [object undefined].
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3.4K views
Problem : I am just starting coding using the nodejs with express. So I simply did this in my file test.js which is into my folder routes : const myexpress = require('express'); const myrouter = new myexpress.Router(); myrouter.get('/test', (req, res) => { res.send(` ... browser, it tells me it Cannot get/test I don't understand why this is happening to me. Please let me know how can I fix this?
asked Jan 23, 2020 jwilliam 3.9k points
0 votes
1 answer 16 views
Problem: Any ideas on what I need to do to fix this? Thanks! Typeerror: app.use() requires a middleware function
asked Mar 16 ummesalma 22k points
0 votes
1 answer 5 views
Problem: I am using node with express + mongoose and trying to use passport.js with restful api. I keep getting this exception after authentication success (I see the callback url on the browser): Passport.initialize() middleware not in use
asked Apr 2 ummesalma 22k points
0 votes
1 answer 3 views
Problem I got an error in the console when I'm executing the node code: SyntaxError: expected expression, got '<' What's I&rsquo;m missing here ?
asked Mar 22 ummesalma 22k points
1 vote
2 answers 875 views
Problem : I am very new to the Node.js and I am having few issues. I am using the Node.js 4.10 and Express 2.4.3. While trying to execute my code I am receiving below error : Error: Can't render headers after they are sent to the client. ... , scope: 'offline_access,email,user_about_me,user_activities,manage_pages,publish_stream', failedUri: '/noauth' }) ])); realapp.use(realapp.router); }); . . .
asked Dec 12, 2019 alecxe 7.5k points