paulby
paulby

Reputation: 87

Deploying node js app to Heroku

I've searched numerous stackoverflow q/a's and still can't seem to solve this. I'm trying to deploy my node js app to Heroku, and keep getting errors. I can run it on my local just fine.

Error:

2014-10-17T13:47:11.174606+00:00 heroku[web.1]: State changed from crashed to starting
2014-10-17T13:47:13.312806+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-10-17T13:47:14.494495+00:00 app[web.1]: module.js:340
2014-10-17T13:47:14.493848+00:00 app[web.1]: 
2014-10-17T13:47:14.504444+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:338:15)
2014-10-17T13:47:14.504440+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2014-10-17T13:47:14.494889+00:00 app[web.1]:     throw err;
2014-10-17T13:47:14.495090+00:00 app[web.1]:           ^
2014-10-17T13:47:14.504450+00:00 app[web.1]:     at node.js:906:3
2014-10-17T13:47:14.504449+00:00 app[web.1]:     at startup (node.js:119:16)
2014-10-17T13:47:14.504447+00:00 app[web.1]:     at Function.Module.runMain (module.js:497:10)
2014-10-17T13:47:14.504446+00:00 app[web.1]:     at Function.Module._load (module.js:280:25)
2014-10-17T13:47:15.551015+00:00 heroku[web.1]: State changed from crashed to starting
2014-10-17T13:47:15.550391+00:00 heroku[web.1]: State changed from starting to crashed
2014-10-17T13:47:15.538661+00:00 heroku[web.1]: Process exited with status 8
2014-10-17T13:47:18.391309+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-10-17T13:47:19.331140+00:00 app[web.1]:           ^
2014-10-17T13:47:19.333015+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2014-10-17T13:47:19.333019+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:338:15)
2014-10-17T13:47:19.330847+00:00 app[web.1]: module.js:340
2014-10-17T13:47:19.330979+00:00 app[web.1]:     throw err;
2014-10-17T13:47:19.333020+00:00 app[web.1]:     at Function.Module._load (module.js:280:25)
2014-10-17T13:47:19.333021+00:00 app[web.1]:     at Function.Module.runMain (module.js:497:10)
2014-10-17T13:47:19.333023+00:00 app[web.1]:     at startup (node.js:119:16)
2014-10-17T13:47:19.333024+00:00 app[web.1]:     at node.js:906:3
2014-10-17T13:47:19.330240+00:00 app[web.1]: 
2014-10-17T13:47:20.165015+00:00 heroku[web.1]: Process exited with status 8
2014-10-17T13:47:20.182699+00:00 heroku[web.1]: State changed from starting to crashe

My app.js file:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

app.get('/', function(req, res) {
  res.render('index', { title: 'The index page!' })
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;

my route/index.js file:

var express = require('express');
var router = express.Router();


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

module.exports = router;

Any help is greatly appreciated

Upvotes: 3

Views: 4926

Answers (1)

ldmat
ldmat

Reputation: 1041

By default Heroku looks for index.js to start your app. You're using app.js (this is the default name given by Express generator, which you're using).

You can either change the name, or specify your custom name with a Procfile.

To create a Procfile, simply create a new file named Procfile (no extension) in your main directory. It should contain this line:

web: node app.js

Upvotes: 7

Related Questions