3

I'm trying to run Mike Wilson's book sample app, but receiving 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'm running the latest code from the book's repo.

what should i do in order to run this sample app properly?

thanks in advance.

7
  • 1
    Three things: Why are you using sudo to run this? Just edit the port definition if need be to be over 3000 for testing, as you're going to be testing against localhost 9 times out of 10. Next, what chapter of which repo are you running? Lastly: try console.log(require('./routes/' + routeName)) Commented Jan 30, 2013 at 17:04
  • What's the value of routeName? It seems like it's not resolving to a file that exists in the /routes/ directory. Commented Jan 30, 2013 at 18:22
  • @jcolebrand i) next time i could not use sudo; ii) i'm using por 8080 because i'm following along the book instructions; iii) i'm running the chapter 9 from the updated repo (not the original book repo). i've tried this command, but doesn't work... Commented Feb 2, 2013 at 14:43
  • "next time I could not use sudo" what? By using a port over 3000 you don't need to use sudo to run your webserver, because it doesn't need special privileges at that point. Commented Feb 3, 2013 at 1:01
  • Where precisely did you try that command? Commented Feb 3, 2013 at 1:01

2 Answers 2

8
+25

You likely have .js files in the routes directory that do not export a function.

app.js is calling require on all files in the routes directory and then calling them as a function. So if any of those files do not follow the general pattern below you'll get the error you're seeing:

module.exports = function(app, models) {
  // Add this file's routes using app.get, etc. calls.
  ...
};
Sign up to request clarification or add additional context in comments.

3 Comments

inside /routes i have 5 .js files; 2 of them do not initilize with the above pattern. including this on that files, the app initialize and the console do not show any error; but i cannot load the app, the index.html... the browser only shows me a blank screen...
the demo code app.js file has the following route setup app.get('/', function(req, res){ res.render('index.jade'); }); Have you overridden that in any of your route definitions? i.e. do you have something like //myroutes.js app.get('/', function(req, res){ //some of your logic });
@pcassiano Good to hear that part worked. Looks like this app uses primarily JavaScript views, so bring up your browser's developer tools (e.g. Firebug) to see if there are JavaScript errors.
1

The error reports that the value of require('./routes/' + routeName) is not a function. So you have only one real possible source of the problem.

If './routes/'+routeName doesn't exist, node should throw an error (in v0.8.16 at least), so that isn't it. So the most obvious one is that the module being loaded doesn't export a function (as the error suggests).

You should run console.log('routeName: ', routeName) right before the require statement that is on line 60 of app.js and try running it again. Then, after finding the value of routeName, look in the file it's trying to open. Odds are either module.exports is either exporting an object (or an array, string etc), or is not set at all.

If you're adding routes in the routes folder, take care to follow the same exporting style as the author, namely module.exports = function(app, models) {...}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.