简体   繁体   English

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

[英]Node.js app works locally but heroku says missing module

I made a simple chat application using Node.JS and Socket.IO, everything works fine locally, but when I push it to heroku it gives me an application error, when I check the logs, this is the error:我使用 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>

So I figured out that indexof is a module that Socket.IO uses, and it is in my node_modules folder, but for some reason either it's not being pushed to heroku or it's just not being recognized.所以我发现 indexof 是 Socket.IO 使用的一个模块,它在我的 node_modules 文件夹中,但由于某种原因,它没有被推送到 heroku 或者只是没有被识别。 I reinstalled my modules 5-6 times and recreated the app but it's still giving me the same error.我重新安装了我的模块 5-6 次并重新创建了应用程序,但它仍然给我同样的错误。 My package.json file has 3 dependencies: Express, Socket.IO and Jade我的 package.json 文件有 3 个依赖项:Express、Socket.IO 和 Jade

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

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

I had the same problem , please read it carefully我遇到了同样的问题,请仔细阅读

These are solutions to socket.io related problems这些是socket.io相关问题的解决方案

I hope i will work我希望我会工作

  1. Ports in your (index.js or server.js) & (index.html and your client.js) port must be different.您的(index.js 或 server.js)和(index.html 和您的 client.js)端口中的端口必须不同。 (refer below code) (参考下面的代码)

=============your index.js file ====================== ==============您的 index.js 文件 ======================

(port here is 8000) (这里的端口是 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()) 

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

port here is 8080这里的端口是 8080

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

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

port here is 8080这里的端口是 8080

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

Remember your "server.js or index.js" port should be different from "client.js" port (Rememeber this is important)记住你的“server.js 或 index.js”端口应该与“client.js”端口不同(记住这很重要)

(index.html and your client.js) port must be same (index.html 和您的 client.js) 端口必须相同

  1. You should always use 'http' while working with socket.io (refer above code)使用 socket.io 时应始终使用“http”(请参阅​​上面的代码)

  2. U may not included cors as it allows u to have more resourses , without cors heroku prevent some dependencies to not install in heroku (refer above code)你可能不包括 cors 因为它允许你有更多的资源,没有 cors heroku 防止某些依赖项不在 heroku 中安装(请参阅上面的代码)

  3. Try replacing "io" to "io.connect"尝试将“io”替换为“io.connect”

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

  4. Must write tag at the end in the HTML必须在 HTML 的末尾写标签

  5. U may forget to add this code which is must in "socket.io"你可能忘记添加这个必须在“socket.io”中的代码

It is required in your html file它在您的 html 文件中是必需的

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

  2. This should be in package.json 's scripts这应该在 package.json 的脚本中

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

I am not talking about nodemon , use simple node over here我不是在谈论 nodemon ,在这里使用简单的 node

  1. May be version is creating a problem , u can avoid it by copying all "devDependencies" to "dependencies" in "package.json" and put "*" in version like this可能是版本造成了问题,您可以通过将所有“devDependencies”复制到“package.json”中的“dependencies”并在这样的版本中放置“*”来避免它

    "dependencies": { “依赖关系”:{

    "cors": "*", "cors": "*",

    "express": "*", “表达”: ”*”,

    "nodemon": "*", "nodemon": "*",

    "socket.io": "*" “socket.io”:“*”

    }, },

    "devDependencies": {} “devDependencies”:{}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于缺少`cleanModifiedSubpaths`,Node.js服务器在本地工作,但无法在Heroku上工作 - Node.js server works locally but not on Heroku because of missing `cleanModifiedSubpaths` 应用程序在本地工作,在Heroku上运行 - Node.JS - App works locally, crashes on Heroku - Node.JS Heroku Node.js 应用程序在本地运行,但在部署时不运行 - Heroku Node.js App Works Locally but not when deployed node.js 应用程序在本地运行但在部署到 heroku 时崩溃 - node.js app works locally but crashes when deployed on heroku 在Heroku上使用快速应用程序崩溃的Node.js在本地工作 - Node.js with express app crashes on Heroku, works locally Node.js + Mongoose在本地工作,但不在Heroku上工作 - Node.js + Mongoose works locally, but not on Heroku Node.js 可以在本地运行,但在 Heroku 上不能运行一次 - Node.js works locally but not once on Heroku Heroku Node.js(express.js)应用程序在本地运行,但在使用MongoDB时在heroku上失败 - Heroku Node.js (express.js) App Works Locally but failed on heroku when using MongoDB 在 Heroku 上部署 Node.js 应用程序在本地成功,但显示错误:无法在线找到模块“/应用程序/服务器” - Deploying Node.js app on Heroku successful on locally but shows Error: Cannot find module '/app/server' online 使用socket.io和Node.js的应用程序在本地工作,但未部署到heroku - App using socket.io and Node.js works locally but not deployed to heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM