简体   繁体   中英

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!

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.

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 .

您缺少在其他任何地方都拥有的__dirname

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM