javascript - typeError when passing in objects to require() for passport -
i typeerror: object not function
when run:
node server.js
for following line of code
require('./app/routes.js')(app, passport);
from following code server.js file:
// server.js // set ====================================================================== // tools need var express = require('express'); var app = express(); var port = process.env.port || 8080; var mongoose = require('mongoose'); var passport = require('passport'); var flash = require('connect-flash'); var morgan = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var session = require('express-session'); var configdb = require('./config/database.js'); // configuration ================================================================ mongoose.connect(configdb.url); // connect our database // require('./config/passport')(passport); // pass passport configuration // set our express application app.use(morgan('dev')); // log every request console app.use(cookieparser()); // read cookies (needed auth) app.use(bodyparser.json()); // info html forms app.use(bodyparser.urlencoded({ extended: true})); app.set('view engine', 'ejs'); // set ejs templating // requirements passport: app.use(session({ secret: 'ilovescotchscotchyscotchscotch'})); app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash flash messages stored in session // routes ======================================================================== require('./app/routes.js')(app, passport); // load our routes , pass in our app , configured passport // launch ======================================================================= app.listen(port); console.log('the magic happens on port ' + port);
not entirely sure why happening...
i following tutorial btw: https://scotch.io/tutorials/easy-node-authentication-setup-and-local
thanks!
based on comments: post pretty job of explaining how require works in nodejs (it's not javascript thing). every file can required expected export requiring file can assign variable (e.g. var somevar = require('somefile');
). if exported value function can invoke before assigning variable (e.g. var somevar = require('somefile')(params);
). error appears file ./app/routes.js
not exporting function.
file: ./app/routes.js
// code cool stuff // ... // time expose whatever want requiring file module.exports = someobject; // or value, function, etc.
file: main.js
// want cool stuff routes.js var coolstuff = require('./app/routes.js'); // someobject ./app/routes.js assigned coolstuff coolstuff(); // error: object not function console.log(typeof coolstuff) // "object"