Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

node.js - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...