无法在Heroku上成功执行Node.js应用程序(每次都崩溃)

5
我尝试根据Heroku的说明在Node.js应用程序上搭建一个样例,并将其部署到Heroku上。这里是他们的说明:https://devcenter.heroku.com/articles/nodejs 当我使用foreman start在本地运行应用程序时,一切都正常。但是,每次我部署应用程序时,它就会崩溃。我做错了什么? Procfile文件内容如下:
web: node web.js

我的 package.json 文件包含以下内容:

{
  "name": "testapp",
  "version": "0.0.1",
  "engines": {
      "node": "0.6.15"
    , "npm": "1.1.9"    
  }
  , "dependencies": {
    "tower": "0.4.0-12"
  }
}

我的web.js包含如下内容:

var express = require('express');

var app = express.createServer(express.logger());

app.get('/', function(request, response) {
  response.send('Hello World!');
});

var port = process.env.PORT || 3000;
app.listen(port, function() {
  console.log("Listening on " + port);
});

应用程序部署并启动,但每次都崩溃。我从应用程序中看到的日志输出如下:
2012-04-27T20:21:31+00:00 heroku[web.1]: State changed from created to starting
2012-04-27T20:21:37+00:00 heroku[web.1]: Starting process with command `node web.js`
2012-04-27T20:21:38+00:00 app[web.1]: 
2012-04-27T20:21:38+00:00 app[web.1]: node.js:201
2012-04-27T20:21:38+00:00 app[web.1]:         throw e; // process.nextTick error, or 'error' event on first tick
2012-04-27T20:21:38+00:00 app[web.1]:               ^
2012-04-27T20:21:38+00:00 app[web.1]: Error: Cannot find module 'express'
2012-04-27T20:21:38+00:00 app[web.1]:     at Function._resolveFilename (module.js:332:11)
2012-04-27T20:21:38+00:00 app[web.1]:     at Function._load (module.js:279:25)
2012-04-27T20:21:38+00:00 app[web.1]:     at Module.require (module.js:354:17)
2012-04-27T20:21:38+00:00 app[web.1]:     at require (module.js:370:17)
2012-04-27T20:21:38+00:00 app[web.1]:     at Object.<anonymous> (/app/web.js:1:77)
2012-04-27T20:21:38+00:00 app[web.1]:     at Module._compile (module.js:441:26)
2012-04-27T20:21:38+00:00 app[web.1]:     at Object..js (module.js:459:10)
2012-04-27T20:21:38+00:00 app[web.1]:     at Module.load (module.js:348:31)
2012-04-27T20:21:38+00:00 app[web.1]:     at Function._load (module.js:308:12)
2012-04-27T20:21:38+00:00 app[web.1]:     at Array.0 (module.js:479:10)
2012-04-27T20:21:39+00:00 heroku[web.1]: Process exited with status 1
2012-04-27T20:21:40+00:00 heroku[web.1]: State changed from starting to crashed
2012-04-27T20:30:01+00:00 heroku[router]: Error H10 (App crashed) -> GET testapp.herokuapp.com/ dyno= queue= wait= service= status=503 bytes=
3个回答

10

看起来你在web.js中需要使用express,那么为什么不将express添加到你的依赖项中呢?你本地的副本可能已经安装了express,这就是为什么你没有收到任何错误的原因。Tower可能需要express,但你不能直接从其中访问express,它是tower目录中的子模块,而不是你的应用程序目录中的子模块。


有趣。本地执行并不需要,但这确实解决了问题。你有什么想法关于本地和远程Heroku的差异吗?此外,如何快速获取版本信息列表以添加到package.json中?通常只需使用npm ls即可。试图了解有效地组装package.json的方法。 - ylluminate
1
也许你在电脑上全局安装了 Express?可以使用 npm install express -g 进行安装。npm ls 没问题,我使用 http://package.json.nodejitsu.com/ 作为我的 package.json 组装参考。 - Mustafa
对我来说,这也是我的应用程序无法在Heroku上运行的原因。我没有在package.json中将所有必需的模块列为依赖项。 - user3616725

0
在过去,这是一个与npm版本相关的问题。我建议升级到最新版本并至少重新安装express。

0

请检查 package.json 文件中是否将 express 定义在 devDependencies 中。如果是的话,即使运行 npm install express --save 命令也无法将其移动到 dependencies 中。

我曾在使用 vue-cli 创建的 Vue.js 项目中遇到过这个问题,因为它将 express 添加到了 devDependencies 中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接