Node.js应用程序在本地工作,但heroku说缺少模块

时间:2014-06-02 04:58:20

标签: node.js heroku socket.io

我使用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

3 个答案:

答案 0 :(得分:33)

好吧,所以2小时后我发现了问题,多个文件夹名为&#34; emitter&#34;包含indexof模块的还有一个gitignore文件,让git忽略了模块,不知道为什么会出现这种情况,但删除它们修复了问题

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我遇到了同样的问题,请仔细阅读

<块引用>

这些是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 or index.js”端口应该和“client.js”端口不同(记住这很重要)

<块引用>

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

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

  2. 你可能不包括 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 ,在这里使用简单的 node

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

    “依赖项”:{

    "cors": "*",

    "express": "*",

    "nodemon": "*",

    "socket.io": "*"

    },

    “devDependencies”:{}

相关问题