slackbot39243
slackbot39243

Reputation: 241

typeError when passing in objects to require() for passport

I get typeError: object is not a function

when I run:

node server.js

for the following line of code

require('./app/routes.js')(app, passport);

from the following code for my server.js file:

// server.js

// set up ======================================================================
// get all the tools we 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 to our database

// require('./config/passport')(passport); // pass passport for configuration


// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json()); // get info from html forms
    app.use(bodyParser.urlencoded({ extended: true}));

app.set('view engine', 'ejs'); // set up ejs for templating

// requirements for passport:
app.use(session({ secret: 'ilovescotchscotchyscotchscotch'}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

// routes ========================================================================
require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport

// launch =======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);

not entirely sure why this is happening...

I was following this tutorial btw: https://scotch.io/tutorials/easy-node-authentication-setup-and-local

Thanks!

Upvotes: 1

Views: 927

Answers (1)

Jason Cust
Jason Cust

Reputation: 10909

Based on your comments: this post does a pretty good job of explaining how require works in nodeJS (it's not a Javascript thing). Basically every file that can be required is generally expected to export something that the requiring file can assign to a variable (e.g. var someVar = require('someFile');). IF the exported value is a function then you can immediately invoke it before assigning to that variable (e.g. var someVar = require('someFile')(params);). For your error it appears that the file ./app/routes.js is not exporting a function.

FILE: ./app/routes.js

// some code that does cool stuff
// ...
// Time to expose whatever we want to the requiring file
module.exports = someObject; // Or value, function, etc.

FILE: main.js

// we want cool stuff from routes.js
var coolStuff = require('./app/routes.js');

// someObject from ./app/routes.js is now assigned to coolStuff
coolStuff(); // ERROR: object is not a function

console.log(typeof coolStuff) // "object"

Upvotes: 2

Related Questions