将 Node.js 应用程序部署到 Heroku 时出现应用程序错误

Application Error for deploying Node.js app to Heroku

我不熟悉将 Github 项目部署到 Heroku。到目前为止,我已经成功地将我的应用程序连接到 Heroku 并进行了部署。该项目在我的本地机器上运行良好。但是,当我尝试启动我的应用程序的 heroku 网站时,它给了我这个错误:

"An error occurred in the application and your page could not be served. Please try again in a few moments.

If you are the application owner, check your logs for details."

这是我的 heroku 日志,我无法理解它。

2015-11-10T04:33:10.538175+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=compasss.herokuapp.com request_id=af3a9061-1b8f-47fe-9e2e-eafa624f2712 fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:33:10.649481+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=compasss.herokuapp.com request_id=e8fc1b43-f629-4e38-85c9-808d731e852e fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:33:11.334843+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=compasss.herokuapp.com request_id=149ffc6c-8a57-475c-9f64-5224bcf72bfd fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:33:12.207897+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=compasss.herokuapp.com request_id=4585d320-5d3b-43a0-ab85-a84f9734e373 fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:35:18.757410+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=compasss.herokuapp.com request_id=1d59ddd7-ff62-4b01-8b3a-df450fac5ba5 fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:35:19.737947+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=compasss.herokuapp.com request_id=2f8f9bc8-8299-4da9-bdad-eed9fa537573 fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:35:19.503853+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=compasss.herokuapp.com request_id=77870378-cde5-48e2-b4d2-e93526b1e10d fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:41:09.087924+00:00 heroku[api]: Deploy 5c325e8 by myemail@gmail.com
2015-11-10T04:41:09.087924+00:00 heroku[api]: Release v6 created by myemail@gmail.com
2015-11-10T04:41:09.271289+00:00 heroku[slug-compiler]: Slug compilation started
2015-11-10T04:41:09.271300+00:00 heroku[slug-compiler]: Slug compilation finished
2015-11-10T04:41:11.469002+00:00 heroku[web.1]: State changed from crashed to starting
2015-11-10T04:41:12.469938+00:00 heroku[web.1]: Starting process with command `node app.js`
2015-11-10T04:41:14.322786+00:00 app[web.1]: module.js:339
2015-11-10T04:41:14.322794+00:00 app[web.1]:     throw err;
2015-11-10T04:41:14.322795+00:00 app[web.1]:     ^
2015-11-10T04:41:14.322796+00:00 app[web.1]:
2015-11-10T04:41:14.322797+00:00 app[web.1]: Error: Cannot find module 'express3-handlebars'
2015-11-10T04:41:14.322797+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:337:15)
2015-11-10T04:41:14.322798+00:00 app[web.1]:     at Function.Module._load (module.js:287:25)
2015-11-10T04:41:14.322799+00:00 app[web.1]:     at Module.require (module.js:366:17)
2015-11-10T04:41:14.322799+00:00 app[web.1]:     at require (module.js:385:17)
2015-11-10T04:41:14.322800+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:6:18)
2015-11-10T04:41:14.322800+00:00 app[web.1]:     at Module._compile (module.js:425:26)
2015-11-10T04:41:14.322801+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:432:10)
2015-11-10T04:41:14.322802+00:00 app[web.1]:     at Module.load (module.js:356:32)
2015-11-10T04:41:14.322802+00:00 app[web.1]:     at Function.Module._load (module.js:311:12)
2015-11-10T04:41:14.322803+00:00 app[web.1]:     at Function.Module.runMain (module.js:457:10)
2015-11-10T04:41:15.124877+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-10T04:41:15.116760+00:00 heroku[web.1]: Process exited with status 1
2015-11-10T04:44:48.813133+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=compasss.herokuapp.com request_id=81fddfea-92e6-4c64-91aa-ec5d076fb8e6 fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:44:49.590449+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=compasss.herokuapp.com request_id=efa1b5fc-5a1e-4b31-acef-5a1025e8c99d fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:44:49.955648+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=compasss.herokuapp.com request_id=4af0cf53-ea53-4361-a5d2-6a3efc51a62f fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:51:46.883632+00:00 heroku[api]: Deploy b0f6afe by myemail@gmail.com
2015-11-10T04:51:46.883703+00:00 heroku[api]: Release v7 created by myemail@gmail.com
2015-11-10T04:51:46.979243+00:00 heroku[web.1]: State changed from crashed to starting
2015-11-10T04:51:47.041376+00:00 heroku[slug-compiler]: Slug compilation started
2015-11-10T04:51:47.041386+00:00 heroku[slug-compiler]: Slug compilation finished
2015-11-10T04:51:48.436794+00:00 heroku[web.1]: Starting process with command `node app.js`
2015-11-10T04:51:50.436732+00:00 app[web.1]: module.js:339
2015-11-10T04:51:50.436759+00:00 app[web.1]:     at Function.Module._load (module.js:287:25)
2015-11-10T04:51:50.436761+00:00 app[web.1]:     at Module._compile (module.js:425:26)
2015-11-10T04:51:50.436754+00:00 app[web.1]:     throw err;
2015-11-10T04:51:50.436756+00:00 app[web.1]:
2015-11-10T04:51:50.436763+00:00 app[web.1]:     at Function.Module._load (module.js:311:12)
2015-11-10T04:51:50.436755+00:00 app[web.1]:     ^
2015-11-10T04:51:50.436759+00:00 app[web.1]:     at Module.require (module.js:366:17)
2015-11-10T04:51:50.436757+00:00 app[web.1]: Error: Cannot find module 'express3-handlebars'
2015-11-10T04:51:50.436758+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:337:15)
2015-11-10T04:51:50.436762+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:432:10)
2015-11-10T04:51:50.436764+00:00 app[web.1]:     at Function.Module.runMain (module.js:457:10)
2015-11-10T04:51:50.436760+00:00 app[web.1]:     at require (module.js:385:17)
2015-11-10T04:51:50.436760+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:6:18)
2015-11-10T04:51:50.436762+00:00 app[web.1]:     at Module.load (module.js:356:32)
2015-11-10T04:51:51.157210+00:00 heroku[web.1]: Process exited with status 1
2015-11-10T04:51:52.735586+00:00 heroku[web.1]: Starting process with command `node app.js`
2015-11-10T04:51:55.545983+00:00 app[web.1]: module.js:339
2015-11-10T04:51:55.545998+00:00 app[web.1]:     throw err;
2015-11-10T04:51:55.545999+00:00 app[web.1]:     ^
2015-11-10T04:51:55.545999+00:00 app[web.1]:
2015-11-10T04:51:55.546001+00:00 app[web.1]: Error: Cannot find module 'express3-handlebars'
2015-11-10T04:51:55.546002+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:337:15)
2015-11-10T04:51:55.546002+00:00 app[web.1]:     at Function.Module._load (module.js:287:25)
2015-11-10T04:51:55.546003+00:00 app[web.1]:     at Module.require (module.js:366:17)
2015-11-10T04:51:55.546003+00:00 app[web.1]:     at require (module.js:385:17)
2015-11-10T04:51:55.546004+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:6:18)
2015-11-10T04:51:55.546004+00:00 app[web.1]:     at Module._compile (module.js:425:26)
2015-11-10T04:51:55.546005+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:432:10)
2015-11-10T04:51:55.546006+00:00 app[web.1]:     at Module.load (module.js:356:32)
2015-11-10T04:51:55.546007+00:00 app[web.1]:     at Function.Module.runMain (module.js:457:10)
2015-11-10T04:51:55.546006+00:00 app[web.1]:     at Function.Module._load (module.js:311:12)
2015-11-10T04:51:51.172084+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-10T04:51:51.173050+00:00 heroku[web.1]: State changed from crashed to starting
2015-11-10T04:51:56.492216+00:00 heroku[web.1]: Process exited with status 1
2015-11-10T04:51:56.506380+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-10T04:53:27.766915+00:00 heroku[api]: Deploy 15c64ba by myemail@gmail.com
2015-11-10T04:53:27.766915+00:00 heroku[api]: Release v8 created by myemail@gmail.com
2015-11-10T04:53:27.910792+00:00 heroku[slug-compiler]: Slug compilation started
2015-11-10T04:53:27.910801+00:00 heroku[slug-compiler]: Slug compilation finished
2015-11-10T04:53:27.921730+00:00 heroku[web.1]: State changed from crashed to starting
2015-11-10T04:53:29.221014+00:00 heroku[web.1]: Starting process with command `node app.js`
2015-11-10T04:53:31.264123+00:00 app[web.1]: module.js:339
2015-11-10T04:53:31.264132+00:00 app[web.1]:     throw err;
2015-11-10T04:53:31.264133+00:00 app[web.1]:     ^
2015-11-10T04:53:31.264134+00:00 app[web.1]:
2015-11-10T04:53:31.264135+00:00 app[web.1]: Error: Cannot find module 'express3-handlebars'
2015-11-10T04:53:31.264136+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:337:15)
2015-11-10T04:53:31.264136+00:00 app[web.1]:     at Function.Module._load (module.js:287:25)
2015-11-10T04:53:31.264137+00:00 app[web.1]:     at Module.require (module.js:366:17)
2015-11-10T04:53:31.264138+00:00 app[web.1]:     at require (module.js:385:17)
2015-11-10T04:53:31.264138+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:6:18)
2015-11-10T04:53:31.264139+00:00 app[web.1]:     at Module._compile (module.js:425:26)
2015-11-10T04:53:31.264140+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:432:10)
2015-11-10T04:53:31.264140+00:00 app[web.1]:     at Module.load (module.js:356:32)
2015-11-10T04:53:31.264141+00:00 app[web.1]:     at Function.Module._load (module.js:311:12)
2015-11-10T04:53:31.264141+00:00 app[web.1]:     at Function.Module.runMain (module.js:457:10)
2015-11-10T04:53:31.969583+00:00 heroku[web.1]: Process exited with status 1
2015-11-10T04:53:31.989169+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-10T04:54:44.812224+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=compasss.herokuapp.com request_id=a508a64d-8a85-4d09-a610-67ef57854186 fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:54:45.733283+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=compasss.herokuapp.com request_id=1a312b38-8793-41fa-9a77-2d2e7f0c63f2 fwd="138.110.234.184" dyno= connect= service= status=503 bytes=
2015-11-10T04:54:45.461863+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=compasss.herokuapp.com request_id=47d48dc9-3e16-4b49-8b25-7b56a8b3ccad fwd="138.110.234.184" dyno= connect= service= status=503 bytes=

我认为错误可能是由于我设置路线的方式造成的。这是我的 app.js,我在其中指定路线:

var express = require('express');
var app = express();
var http = require('http').Server(app);

// set up handlebars view engine
var handlebars = require('express3-handlebars')
  .create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

http.listen(process.env.PORT || 3000, function(){
  console.log('listening on', http.address().port);
})

// Home Page
app.get('/', function(req, res){
  res.render('home');
});

// About Page
app.get('/about', function(req, res){
  res.render('about');
});

// 404 catch-all handler (middleware)
app.use(function(req, res){
  res.status(404);
  res.render('404');
});

// 500 error handler (middleware)
app.use(function(req, res){
  console.error(err.stack);
  res.status(500);
  res.render('500');
});

app.listen(app.get('port'), function(){
console.log( 'Express started on http://localhost:' +
app.get('port') + '; press Ctrl-C to terminate.' );
});

这是我的 package.json:

{
  "name": "Compass",
  "version": "1.0.0",
  "description": "Independent Study Application",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "Thao Bach",
  "license": "ISC",
  "dependencies": {
    "express": "^4.13.3",
    "express-handlebars": "^2.0.1",
    "mongodb": "^2.0.46",
    "monk": "^1.0.1"
  },
  "devDependencies": {
    "archiver": "^0.15.1",
    "chai": "^3.3.0",
    "fs-extra": "^0.24.0",
    "mocha": "^2.3.3"
  }
}

很可能你没有在你的 package.json 中提到的 npm 依赖项 express3-handlebars 或者它在你的 package.json 文件中作为开发依赖项提到。

此外,在您的 app.js 中,您正在使用

var handlebars = require('express3-handlebars')
  .create({ defaultLayout:'main' });

您可能希望更改您的 require 语句以指向 express-handlebars 或包含 express3-handlebars 作为依赖项