title: How to fix "TypeError: app.use() requires a middleware function"?
body: <p>I am new to <strong>Node.js</strong> and suspect there is an error in my <code>passport.js</code> or <code>signupLogin.js</code> file with the message,</p>
  <pre><code>app.use() requires a middleware function
  </code></pre>
  <p>which I cannot figure out. I believe it's the signupLogin route as before I added this file in, the app was working fine.</p>
  <p>My project folder structure with relevant files is as follows:</p>
  <pre><code>App
  ├── config
  │   ├── database.js
  │   ├── passport.js
  ├── node_modules
  ├── public
  ├── routes
  │   ├── index.js
  │   ├── product_routes.js
  │   ├── signupLogin.js
  ├── views
  ├── app.js
  ├── package-lock.json
  ├── package.json
  </code></pre>
  <p><strong>signupLogin.js:</strong></p>
  <pre><code>var express = require('express');
  var router = express.Router();
  module.exports = function(router, passport) {
      router.get('/', function(req, res){
       res.render('index.ejs');
      });
     
      router.get('/login', function(req, res){
       res.render('login.ejs', {message:req.flash('loginMessage')});
      });
     
      router.post('/login', passport.authenticate('local-login', {
       successRedirect: '/profile',
       failureRedirect: '/login',
       failureFlash: true
      }),
       function(req, res){
        if(req.body.remember){
         req.session.cookie.maxAge = 1000 * 60 * 3;
        }else{
         req.session.cookie.expires = false;
        }
        res.redirect('/');
       });
     
       router.get('/signup', function(req, res){
       res.render('signup.ejs', {message: req.flash('signupMessage')});
      });
     
      router.post('/signup', passport.authenticate('local-signup', {
       successRedirect: '/profile',
       failureRedirect: '/signup',
       failureFlash: true
      }));
     
      router.get('/profile', isLoggedIn, function(req, res){
       res.render('profile.ejs', {
        user:req.user
       });
      });
     
      router.get('/logout', function(req,res){
       req.logout();
       res.redirect('/');
      })
     };
     
     function isLoggedIn(req, res, next){
      if(req.isAuthenticated())
       return next();
     
      res.redirect('/');
     }
  </code></pre>
  <p><strong>app.js:</strong></p>
  <pre><code>var express = require('express');
  var path = require('path');
  var cookieParser = require('cookie-parser');
  var multer = require('multer');
  var bodyParser = require('body-parser');
  //
  var morgan = require('morgan');
  var session = require('express-session');
  var passport = require('passport');
  var flash = require('connect-flash');
  require('./config/passport')(passport);
  
  const app = express();
  const PORT = 3000;
  
  // view engine setup
  app.set('view engine', 'ejs');
  app.set('views', path.join(__dirname, '/views'));
  
  // Add middleware 
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(multer({ storage: multer.memoryStorage({}) }).any());
  app.use(cookieParser());
  app.use(morgan('dev'));
  app.use(cookieParser());
  app.use(bodyParser.urlencoded({
   extended: true
  }));
  app.use(session({
    secret: 'justasecret',
    resave:true,
    saveUninitialized: true
   }));
   
   app.use(passport.initialize());
   app.use(passport.session());
   app.use(flash());
   
  
  
  // Tell the app to use the defined routes from above
  app.use(require('./routes/index'));
  app.use(require('./routes/product_routes'));
  app.use(require('./routes/signupLogin.js')(app, passport));
  
  
  app.listen(PORT, () =&gt; {
    console.log(`The app is running on port ${PORT} at 'http://localhost:${PORT}' (Use CTRL + C to exit)`);
  });
  
  module.exports = app;
  </code></pre>
  <p><strong>passport.js:</strong></p>
  <pre><code>var LocalStrategy = require(&quot;passport-local&quot;).Strategy;
  
  var mysql = require('mysql');
  var bcrypt = require('bcrypt-nodejs');
  var dbconfig = require('./database');
  var connection = mysql.createConnection(dbconfig.connection);
  
  connection.query('USE ' + dbconfig.database);
  
  module.exports = function(passport) {
   passport.serializeUser(function(user, done){
    done(null, user.id);
   });
  
   passport.deserializeUser(function(id, done){
    connection.query(&quot;SELECT * FROM users WHERE id = ? &quot;, [id],
     function(err, rows){
      done(err, rows[0]);
     });
   });
  
   passport.use(
    'local-signup',
    new LocalStrategy({
     usernameField : 'username',
     passwordField: 'password',
     passReqToCallback: true
    },
    function(req, username, password, done){
     connection.query(&quot;SELECT * FROM users WHERE username = ? &quot;, 
     [username], function(err, rows){
      if(err)
       return done(err);
      if(rows.length){
       return done(null, false, req.flash('signupMessage', 'That is already taken'));
      }else{
       var newUserMysql = {
        username: username,
        password: bcrypt.hashSync(password, null, null)
       };
  
       var insertQuery = &quot;INSERT INTO users (username, password) values (?, ?)&quot;;
       connection.query(insertQuery, [newUserMysql.username, newUserMysql.password], function(err, rows) {
            if(err) {
              console.log(err);
              return done(null, err);
            } else {
              newUserMysql.id = rows.insertId;
              return done(null, newUserMysql);
          }
       });
      }
     });
    })
   );
  
   passport.use(
    'local-login',
    new LocalStrategy({
     usernameField : 'username',
     passwordField: 'password',
     passReqToCallback: true
    },
    function(req, username, password, done){
     connection.query(&quot;SELECT * FROM users WHERE username = ? &quot;, [username],
     function(err, rows){
      if(err)
       return done(err);
      if(!rows.length){
       return done(null, false, req.flash('loginMessage', 'No User Found'));
      }
      if(!bcrypt.compareSync(password, rows[0].password))
       return done(null, false, req.flash('loginMessage', 'Wrong Password'));
  
      return done(null, rows[0]);
     });
    })
   );
  };
  </code></pre>
  <p><strong>package.json:</strong></p>
  <pre><code>{
    &quot;name&quot;: &quot;plant-a-tree&quot;,
    &quot;version&quot;: &quot;0.0.0&quot;,
    &quot;private&quot;: true,
    &quot;scripts&quot;: {
      &quot;start&quot;: &quot;start http://localhost:3000 &amp; nodemon node app.js&quot;
    },
    &quot;dependencies&quot;: {
      &quot;bcrypt-nodejs&quot;: &quot;0.0.3&quot;,
      &quot;body-parser&quot;: &quot;^1.19.0&quot;,
      &quot;connect-flash&quot;: &quot;^0.1.1&quot;,
      &quot;cookie-parser&quot;: &quot;~1.4.3&quot;,
      &quot;debug&quot;: &quot;~2.6.9&quot;,
      &quot;ejs&quot;: &quot;~2.5.7&quot;,
      &quot;express&quot;: &quot;~4.16.0&quot;,
      &quot;express-session&quot;: &quot;^1.16.1&quot;,
      &quot;http-errors&quot;: &quot;~1.6.2&quot;,
      &quot;morgan&quot;: &quot;~1.9.0&quot;,
      &quot;multer&quot;: &quot;^1.4.1&quot;,
      &quot;mysql&quot;: &quot;^2.17.1&quot;,
      &quot;nodemon&quot;: &quot;^1.18.11&quot;,
      &quot;passport&quot;: &quot;^0.4.0&quot;,
      &quot;passport-local&quot;: &quot;^1.0.0&quot;
    }
  }
  </code></pre>
  <p><em>This is the full error I am getting:</em></p>
  <pre><code>TypeError: app.use() requires a middleware function
      at Function.use (/Users/charizze/Desktop/PlantATree/node_modules/express/lib/application.js:210:11)
      at Object.&lt;anonymous&gt; (/Users/charizze/Desktop/PlantATree/app.js:47:5)
      at Module._compile (internal/modules/cjs/loader.js:701:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
      at Module.load (internal/modules/cjs/loader.js:600:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
      at Function.Module._load (internal/modules/cjs/loader.js:531:3)
      at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
      at startup (internal/bootstrap/node.js:283:19)
      at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
  </code></pre>