我使用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
发布于 2014-06-02 05:11:00
好的,2小时后我发现了问题,包含模块索引的多个名为"emitter“的文件夹也有一个gitignore文件,使得git忽略了模块,不知道为什么会有这个问题,但是删除它们解决了问题。
发布于 2020-03-25 05:16:33
在我的例子中,我不得不向依赖项中添加几个模块,因为它们只是在dependencies下,而不是在生产基础上构建的。
发布于 2021-05-16 12:26:59
我也有同样的问题,请仔细阅读
这些是socket.io相关问题的解决方案,我希望我能工作。
=============your 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()) =============your client.js文件======================端口是8080
const socket = io.connect('https://localhost:8080/')=============your 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)端口必须相同。
它在html文件中是必需的。
我不是在说无恶魔,这里用简单的节点
https://stackoverflow.com/questions/23987571
复制相似问题