Categories
express javascript node.js

Node error: “Cannot find module ‘routes'”

Update 12/14/15: I see that the next chapter instructs the reader to create the ‘require’ module, so I’ll forge ahead. The book seems to be poorly edited – maybe they rearranged some content and didn’t notice the errors those changes made. Per my original question, below, things are definitely out of order. But even if the book has some errors, it’s forcing me to do outside reading to understand those errors, and post questions to SO, and that’s helping me learn. Thanks for your help, folks!


I’m using the “Web Development with MongoDB and Node.js – Second edition” to learn Node and MongoDB (surprising, right?). The book’s approach seems to be to rush the reader through the creation of an app, and only partially explain things along the way. So when something goes wrong, I don’t understand why it went wrong or how to fix it. Also, I’ve found a couple of code typos in the book so far, so maybe one of those is causing my problem.

Right now, I’m trying to run the server.js file the book has had me create, and I’m running into an error. I’m still very new to Node, Express, and full stack generally, so the error could be because of something really simple and obvious that I’m missing. Help!

My folder structure is:
Folder Structure
The Node Modules folder has a bunch of folders, which were created when I ran this command:
npm install express morgan body-parser cookie-parser method-override errorhandler express-handlebars –save

So far, I have two files, the contents of which were copied straight from the book:
server.js:

var express = require('express'),
config = require('./server/configure'),
app = express();
app.set('port', process.env.PORT || 3300);
app.set('views', __dirname + '/views');
app = config(app);
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(app.get('port'), function(){
console.log('Server up: http://localhost:' + app.get('port'));
});

And configure.js, inside the /server/ folder. The code for this file, in the book, appeared to have two typos, which I’ve corrected in this file. (But maybe I missed others, or “corrected” those incorrectly.)

var path = require('path'),
routes = require('./routes'),
exphbs = require('express-handlebars'),
express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
morgan = require('morgan'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler');
module.exports = function(app){
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended': true}));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser('some-secret-value-here'));
routes(app); //moving the routes to routes folder
app.use('/public/',
express.static(path.join(__dirname, '../public')));
if('development' === app.get('env')){
app.use(errorHandler());
}
return app;
};

In a terminal, I enter node server.js. I get an error:
error returned by trying to run server.js

In the configure.js file, I think I understand why I’m getting the error – the routes = require('./routes'), statement seems to be requiring a module that doesn’t exist yet, because the book hasn’t described how to create that, and it doesn’t seem to have been installed via the earlier npm command. But, I’m new at this (as I keep saying) so I might be wrong about that. Can anyone help me figure out what’s going wrong here?

Also, if you happen to want to recommend a good book for learning Express, I’m all ears – maybe I should just ditch this book and start with one that teaches more of the fundamentals before jumping into creating a sample app.