Heroku应用程序错误的Node.js聊天室

时间:2017-03-27 05:25:57

标签: node.js git heroku socket.io

我在加载myoku页面时遇到应用程序错误。我按照https://devcenter.heroku.com/articles/deploying-nodejs中的说明进行操作。

我的代码是此版本https://enlight.ml/nodejs-chat的调整版本。

我的节点server.js文件的一部分。

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var people = {};
var heads = [];
app.get('/', function(req, res, next) {
    res.sendFile(__dirname + '/public/index.html')
});

server.listen(process.env.PORT || 5000);

heroku log。

2017-03-27T05:40:05.790068+00:00 heroku[web.1]: State changed from crashed to starting
2017-03-27T05:40:07.296635+00:00 heroku[web.1]: Starting process with command `node server.js`
2017-03-27T05:40:09.867374+00:00 app[web.1]: module.js:471
2017-03-27T05:40:09.867387+00:00 app[web.1]:     throw err;
2017-03-27T05:40:09.867387+00:00 app[web.1]:     ^
2017-03-27T05:40:09.867388+00:00 app[web.1]: 
2017-03-27T05:40:09.867389+00:00 app[web.1]: Error: Cannot find module 'socket.io'
2017-03-27T05:40:09.867390+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:469:15)
2017-03-27T05:40:09.867390+00:00 app[web.1]:     at Function.Module._load (module.js:417:25)
2017-03-27T05:40:09.867391+00:00 app[web.1]:     at Module.require (module.js:497:17)
2017-03-27T05:40:09.867392+00:00 app[web.1]:     at require (internal/module.js:20:19)
2017-03-27T05:40:09.867392+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:4:30)
2017-03-27T05:40:09.867393+00:00 app[web.1]:     at Module._compile (module.js:570:32)
2017-03-27T05:40:09.867393+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:579:10)
2017-03-27T05:40:09.867395+00:00 app[web.1]:     at Function.Module._load (module.js:438:3)
2017-03-27T05:40:09.867394+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-03-27T05:40:09.867394+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-03-27T05:40:09.947727+00:00 heroku[web.1]: State changed from starting to crashed
2017-03-27T05:40:09.928089+00:00 heroku[web.1]: Process exited with status 1

的package.json。

{
  "name": "im",
  "version": "1.0.0",
  "description": "Messaging App",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/username/im.git"
  },
  "author": "me",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/username/im/issues"
  },
  "homepage": "https://github.com/username/im#readme",
  "dependencies": {
    "express": "4.15.2"
  },
  "engines": {
    "node": "6.10.0"
   },
  "devDependencies": {},
  "keywords": [
    "chat",
    "app"
  ]

}

.gitignore文件。

    /node_modules
    npm-debug.log
   .DS_Store
   /*.env

2 个答案:

答案 0 :(得分:0)

这解决了它。

npm install socket.io --save 
git add . 
git push heroku master

答案 1 :(得分:0)

您需要安装socket.io包并将其保存在package.json

npm install socket.io --save

或参考你保存的地方。

var io = require('../lib/socket.io');

然后使用它。