Srini Srini - 4 months ago 119
Node.js Question

Cannot find module in Nodejs - module.js

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

$ node server.js
module.js:339
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

server.js:

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(morgan('dev'));
app.use(bodyParser.json());
app.use(methodOverride());
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') {
app.use(errorHandler());
}

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

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);
next();
});

/*
Login
*/
app.post('/login', routes.users.login);
app.post('/user/register', routes.users.register);
app.get('/me', routes.users.me);

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

process.on('uncaughtException', function(err) {
console.log(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

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.