Chris
Chris

Reputation: 993

My routes index.js file gives error (Module not found)

The error says "Cannot find module './routes/index'" but clearly it is in that folder (even when static is set to that folder)

Here is the error:

root@ip*censored*:/home/ubuntu/*censored*# module.js:471
    throw err;
    ^

Error: Cannot find module './routes/index'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ubuntu/*censored*/routes/index.js:7:14)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

Here is what my directory looks like:

-bin
-forbiddenDirectory
-Node_modules
-public
-routes
-----index.js
-----users.js
-views
app.js
main.js
bundle.js
package.json

This is in my app.js:

var routes = require("./routes/index");
app.use('/', routes);

This is in my index.js:

var express = require('express');
var router = express.Router();
var app = express();
var aws = require('aws-sdk');
var request = require('request');
var cheerio = require('cheerio');
var routes = require('./routes/index');
var fs = require('fs');
var url = "http://news.google.com";
var ep = new aws.Endpoint('censored.us-west-1.censored.com');
var db = new aws.DynamoDB.DocumentClient({      // Dynamo database constructor
    "apiVersion": '2012-08-10',
    "region": 'us-west-1',
    "endpoint": ep,
    "accessKeyId": 'censored',
    "secretAccessKey": '+censored/censored'
});
var dateObj = new Date();
var dbTimeStamp = dateObj.toLocaleString();

// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
  next();
});


/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});

//module.exports = router;
module.exports = function(req,res,next) { res.render('index')}

So it is clearly in the routes folder but it doesn't read it for some reason. I tried everything... I tried changing ./routes/index to ./routes/index.js, to ./routes/, then ./routes, then /routes, then tried routes/index/, then did ../routes/index same with .js afterwards. I give up and need your guys help. Thank you.

Upvotes: 0

Views: 4633

Answers (4)

Chandan S
Chandan S

Reputation: 173

Check the file extension and file name of the module file

Upvotes: -1

IftekharDani
IftekharDani

Reputation: 3729

Please remove the routes = require('./routes/index'); from index.js

Upvotes: 1

farhadamjady
farhadamjady

Reputation: 982

you should use this code in index.js :

exports.index=function(req,res,next){
  res.render('index');
}

in your app.js :

app.use('/', routes.index);

Upvotes: 1

user5364144
user5364144

Reputation:

Try adding file extension. So your code in app.js will be:

var routes = require("./routes/index.js"); 
app.use('/', routes);

Upvotes: 4

Related Questions