【问题标题】:Node.js app works locally but heroku says missing moduleNode.js 应用程序在本地工作,但 heroku 说缺少模块
【发布时间】:2014-07-22 03:50:20
【问题描述】:

我使用 Node.JS 和 Socket.IO 制作了一个简单的聊天应用程序,在本地一切正常,但是当我将它推送到 heroku 时,它给了我一个应用程序错误,当我检查日志时,这是错误:

Error: Cannot find module 'indexof'
    at Function.Module._resolveFilename <module.js:338:15>
    at Function.Module._load <module.js:280:25>
    at Module.require <module.js:364:17>
    at require <module.js:380:17>
    at Object.<anonymous> </app/node_modules/socket.io/node_modules/socket.io-parser/node_modules/emitter/index.js:6:13>
    at Module._compile <module.js:456:26>
    at Object.Module._extensions..js <module.js:474:10>
    at Module.load <module.js:356:32>
    at Functin.Module._load <module.js:312:12>
    at Module.require <module.js:364:17>

所以我发现 indexof 是 Socket.IO 使用的一个模块,它位于我的 node_modules 文件夹中,但由于某种原因,它要么没有被推送到 heroku,要么就是没有被识别。我重新安装了我的模块 5-6 次并重新创建了应用程序,但它仍然给我同样的错误。我的 package.json 文件有 3 个依赖项:Express、Socket.IO 和 Jade

【问题讨论】:

    标签: 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 的脚本中

      "start":"node index.js",

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

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

      “依赖”:{

      "cors": "*",

      “快递”:“*”,

      "nodemon": "*",

      “socket.io”:“*”

      },

      “devDependencies”:{}

    【讨论】:

      【解决方案2】:

      在我的情况下,我必须向依赖项添加一些模块,因为它们只是在 devdependecies 下,而不是在生产环境中构建

      【讨论】:

        【解决方案3】:

        好吧,所以在 2 小时后我发现了问题,包含 indexof 模块的多个名为“emitter”的文件夹也有一个 gitignore 文件,它使 git 忽略该模块,不知道为什么它甚至在那里,但删除它们修复了问题

        【讨论】:

        • 我遇到了完全相同的问题。然后我用git add -A --force它解决了。
        • 这里一样。为什么会发生这种情况
        • 这里完全一样 ;-) 你为我节省了很多时间
        • 错误的.gitignore:node_modules/socket.io/node_modules/socket.io-adapter/node_modules/socket.io-parser/node_modules/emitter/.gitignore
        猜你喜欢
        • 2015-07-24
        • 1970-01-01
        • 2018-03-04
        • 2017-11-16
        • 2020-12-08
        • 2021-03-05
        • 1970-01-01
        • 2016-10-18
        • 1970-01-01
        相关资源
        最近更新 更多