【问题标题】:Heroku says : Cannot find module 'socket.io'Heroku 说:找不到模块'socket.io'
【发布时间】:2011-12-13 23:35:45
【问题描述】:

我正在部署一个带有 deps/modules 的 node.js 应用程序,例如:stylus、express、socket.io on heroku。

server.js 的开头

/**
 * Bootstrap app.
 */

// I've tried with and without that line... not sure what it does
require.paths.unshift(__dirname + '/../../lib/');



/**
 * Module dependencies.
 */

// I've tried with "socket.io", "./socket.io" and "Socket.IO"
var express = require('express')
  , stylus = require('stylus')
  , nib = require('nib')
  , sio = require('socket.io');

文件 package.json

{ “名称”:“test.io”,“描述”:“blabla”,“版本”:“0.0.1”,“依赖项”:{ “快递”:“2.3.11” ,“玉”:“0.12.1” ,“手写笔”:“0.13.3” ,“笔尖”:“0.0.8” } }

因此,唯一的 heroku web worker 崩溃了。这是日志:

2011-10-24T09:15:27+00:00 heroku[slugc]: Slug compilation finished
2011-10-24T09:15:35+00:00 heroku[web.1]: Unidling
2011-10-24T09:15:35+00:00 heroku[web.1]: State changed from down to created
2011-10-24T09:15:35+00:00 heroku[web.1]: State changed from created to starting
2011-10-24T09:15:39+00:00 heroku[web.1]: State changed from starting to crashed
2011-10-24T09:15:39+00:00 heroku[web.1]: State changed from crashed to created
2011-10-24T09:15:39+00:00 heroku[web.1]: State changed from created to starting
2011-10-24T09:15:41+00:00 heroku[web.1]: Starting process with command `node app.js`
2011-10-24T09:15:41+00:00 app[web.1]: 
2011-10-24T09:15:41+00:00 app[web.1]: node.js:134
2011-10-24T09:15:41+00:00 app[web.1]:         throw e; // process.nextTick error, or 'error' event on first tick
2011-10-24T09:15:41+00:00 app[web.1]:         ^
2011-10-24T09:15:41+00:00 app[web.1]: Error: Cannot find module 'socket.io'
2011-10-24T09:15:41+00:00 app[web.1]:     at Function._resolveFilename (module.js:320:11)
2011-10-24T09:15:41+00:00 app[web.1]:     at Function._load (module.js:266:25)
2011-10-24T09:15:41+00:00 app[web.1]:     at require (module.js:348:19)
2011-10-24T09:15:41+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:18:11)
2011-10-24T09:15:41+00:00 app[web.1]:     at Module._compile (module.js:404:26)
2011-10-24T09:15:41+00:00 app[web.1]:     at Object..js (module.js:410:10)
2011-10-24T09:15:41+00:00 app[web.1]:     at Module.load (module.js:336:31)
2011-10-24T09:15:41+00:00 app[web.1]:     at Function._load (module.js:297:12)
2011-10-24T09:15:41+00:00 app[web.1]:     at Array.<anonymous> (module.js:423:10)
2011-10-24T09:15:41+00:00 app[web.1]:     at EventEmitter._tickCallback (node.js:126:26)
2011-10-24T09:15:41+00:00 heroku[web.1]: Process exited
2011-10-24T09:15:44+00:00 heroku[web.1]: State changed from starting to crashed

那么,有什么想法吗?有人遇到过吗?

【问题讨论】:

    标签: node.js heroku socket.io


    【解决方案1】:

    这些是socket.io相关问题的解决方案

    我希望我会工作

    1. (index.js 或 server.js)和(index.html 和您的 client.js)端口中的端口必须不同。 (参考下面的代码)

    =============你的 index.js 文件 ======================

    (这里的端口是8000)

    const express = require("express")
    var app = express();
    const http = require('http')
    var server = http.createServer(app);
      
    const port = process.env.PORT || 8000
    server.listen(port,()=>
    {
        console.log("Listening at port => "+port)
    });
    var io = require('socket.io')(server, {
        cors: {
          origin: '*',
        }
    });
    
    const cors = require("cors")
    app.use(cors()) 
    

    =============你的client.js文件======================

    这里的端口是 8080

    const socket = io.connect('https://localhost:8080/')
    

    =============您的 index.html 文件 ======================

    这里的端口是 8080

     <script defer src="https://localhost:8080/socket.io/socket.io.js"> 
     </script>
    

    请记住您的“server.js 或 index.js”端口应该与“client.js”端口不同(记住这一点很重要)

    (index.html 和你的 client.js)端口必须相同

    1. 在使用 socket.io 时,您应该始终使用“http”(参考上面的代码)

    2. U 可能不包含 cors,因为它允许你拥有更多资源,没有 cors heroku 会阻止某些依赖项无法安装在 heroku 中(请参阅上面的代码)

    3. 尝试将“io”替换为“io.connect”

      const socket = io.connect('https://localhost:8080/')

    4. 必须在HTML末尾写标签

    5. 你可能会忘记添加这个必须在“socket.io”中的代码

    在您的 html 文件中是必需的

    1. 删除“node_modules”和“package-lock.json” 并在 cmd 中写入“npm i”

    2. 这应该在 package.json 的脚本中

      "开始":"节点 index.js",

    我不是在说 nodemon ,这里使用简单的节点

    1. 可能是版本造成了问题,你可以通过将所有“devDependencies”复制到“package.json”中的“dependencies”并将“*”放在这样的版本中来避免它

      “依赖”:{

      "cors": "*",

      “快递”:“*”,

      "nodemon": "*",

      “socket.io”:“*”

      },

      “devDependencies”:{}

    【讨论】:

      【解决方案2】:

      我相信你应该在 package.json 的依赖项中添加 socket.io。

      { "name": "test.io" , "description": "blabla" , "version": "0.0.1" , "dependencies": { "express": "2.3.11" , "jade": "0.12.1" , "stylus": "0.13.3" , "nib": "0.0.8" , "socket.io" : "0.8.5" } }
      

      或者只是这样做:

      npm install socket.io --save

      这将安装最新版本的 socket.io 并将其添加到依赖项中。

      【讨论】:

        【解决方案3】:
        npm install socket.io 
        

        为我做了诀窍。如果您在 Windows 下,请确保您以管理员身份运行它。

        【讨论】:

          【解决方案4】:

          你确定你安装了socket io吗?尝试运行:

          npm install socket.io
          

          这将安装节点数据包存储库中可用的最新 socket.io。

          【讨论】:

            猜你喜欢
            • 2014-01-02
            • 2014-07-02
            • 2021-02-16
            • 1970-01-01
            • 2015-09-21
            • 1970-01-01
            • 2023-04-03
            相关资源
            最近更新 更多