Srini Srini - 1 year ago 322
Node.js Question

Cannot find module in Nodejs - module.js

I am getting the following error when running the node server:

$ node server.js
throw err;

Error: Cannot find module './server/routes'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (C:\Users\attas\documents\github\angular-express-auth\server.js:12:10)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)

Below is my project structure:

Project structure


var express = require('express'),
jwt = require('express-jwt'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
methodOverride = require('method-override'),
errorHandler = require('express-error-handler'),
tokenManager = require('./server/config/token_manager'),
secret = require('./server/config/secret'),
http = require('http'),
path = require('path');

var app = module.exports = express();

app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'app')));

var routes = require('./server/routes');
routes.users = require('./server/routes/users.js');
var env = process.env.NODE_ENV || 'development';

// development only
if (env === 'development') {

// production only
if (env === 'production') {

app.all('*', function(req, res, next) {
res.set('Access-Control-Allow-Origin', 'http://localhost');
res.set('Access-Control-Allow-Credentials', true);
res.set('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization');
if ('OPTIONS' == req.method) return res.send(200);

*/'/login', routes.users.login);'/user/register', routes.users.register);

app.get('/logout', jwt({secret: secret.secretToken}), routes.users.logout);

process.on('uncaughtException', function(err) {

* Start Server
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));

Not getting any error for

tokenManager = require('./server/config/token_manager') and
secret = require('./server/config/secret')

wondering why it cannot load only ./server/routes ?

Answer Source

When you define a module like this:

var routes = require('./server/routes');

If index.js file is exist in ./server/routes directory, It's meaning you call ./server/routes/index.js file.

You can't call like this: ./server/routes/index.js/users.js.

You should update your users router variable like below:

var routes = require('./server/routes');
var users  = requier('./server/routes/users');

And update users variable's other usages in server.js file, your app will be work correctly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download