Node.js TypeError: object is not a function

I'm trying to run my example app with Mike Wilson's sample, but I get the following error:

pcassiano@...:~/socialnet$ sudo node app.js

/home/pcassiano/socialnet/app.js:60
  require('./routes/' + routeName)(app, models);
                                  ^
TypeError: object is not a function
    at /home/pcassiano/socialnet/app.js:60:35
    at Array.forEach (native)
    at Object.<anonymous> (/home/pcassiano/socialnet/app.js:57:26)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

      

I am running the latest code from the book repo .

what should I do to get this sample application to run correctly?

early.

+3


source to share


2 answers


There are probably files .js

in the directory routes

that don't export the function.

app.js

calls require

for all files in a directory routes

and then calls them as a function. Therefore, if any of these files do not follow the general scheme below, you will receive an error message:



module.exports = function(app, models) {
  // Add this file routes using app.get, etc. calls.
  ...
};

      

+8


source


The error says that the value is require('./routes/' + routeName)

not a function. Thus, you only have one real possible source of the problem.

If it './routes/'+routeName

doesn't exist, node should throw an error (at least in v0.8.16), so it doesn't. So the most obvious thing is that the loadable module does not export the function (as the error suggests).



You should run console.log('routeName: ', routeName)

right before the require statement, which is at line 60 of app.js, and try running it again. Then, after finding the value routeName

, look at the file it is trying to open. The coefficients are either module.exports

- this is either an object export (or an array, a string, etc.), or not set at all.

If you add routes to a routes folder, follow the same export style as the author, namely module.exports = function(app, models) {...}

+1


source







All Articles