Sahar Ben-Shushan
Sahar Ben-Shushan

Reputation: 297

Heroku Cannot find module

I'm trying to require some JS file in Node, Localy - works great, but in Heroku I get this error-

Error: Cannot find module './routes.js'

my code looks like :

'use strict';

var express = require('express');
var app = express();

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));

// Application Header
app.use(function(request, response, next) {
    response.header('Access-Control-Allow-Origin', '*');
    response.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    response.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, X-PINGOTHER');

  next();
});

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

  console.log('./routes.js');
require('./routes.js')(app);

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

What am I doing wrong? Thanx!

Upvotes: 0

Views: 12534

Answers (4)

chucky
chucky

Reputation: 151

I just fixed this. I changed my DB directory to lowercase and for some reason this change didn't push up to github. So in Heroku it could not read the path where the db stuff was located as it's case sensitive it seems.

Upvotes: 0

Manuel
Manuel

Reputation: 15042

Try to disable the Heroku build cache to recreate the node_modules directory.

With the Heroku CLI installed, write:

$ heroku config:set NODE_MODULES_CACHE=false
$ git commit -am 'disable_node_modules_cache' --allow-empty
$ git push heroku master

As described here.

Upvotes: 4

Sydin
Sydin

Reputation: 647

You could try running a one-off copy of your dyno to have it list the directory contents – this would allow you to check if your file is where you'd expect it to be. (Heroku has more information on this here.) For example:

heroku run 'ls -al'

This will cause Heroku to create (very briefly) an additional copy of your application, and have it list the files in your app's directory on the server. You may find that your routes.js file is not where you expect it to be. (Perhaps it's not checked into git?)

If you'd like to poke around further, run:

heroku run bash

And you'll have an interactive bash shell to a copy of your app. From there you can poke around at the filesystem, try running your app manually on the server, etc.

Upvotes: 6

TJC
TJC

Reputation: 727

you're missing the __dirname that you have everywhere else.

Upvotes: 0

Related Questions