[英]Node.js server works locally but not on Heroku because of missing `cleanModifiedSubpaths`
[英]Node.js app works locally but heroku says missing module
我使用 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
好的,所以在 2 小時后我發現了問題,包含 indexof 模塊的多個名為“emitter”的文件夾也有一個 gitignore 文件,該文件使 git 忽略該模塊,不知道為什么它甚至在那里,但刪除它們解決了問題
就我而言,我不得不向依賴項添加一些模塊,因為它們僅在 devdependecies 下,而不是在生產環境中構建
我遇到了同樣的問題,請仔細閱讀
這些是socket.io相關問題的解決方案
我希望我會工作
==============您的 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) 端口必須相同
使用 socket.io 時應始終使用“http”(請參閱上面的代碼)
你可能不包括 cors 因為它允許你有更多的資源,沒有 cors heroku 防止某些依賴項不在 heroku 中安裝(請參閱上面的代碼)
嘗試將“io”替換為“io.connect”
const socket = io.connect('https://localhost:8080/')
必須在 HTML 的末尾寫標簽
你可能忘記添加這個必須在“socket.io”中的代碼
它在您的 html 文件中是必需的
刪除“node_modules”和“package-lock.json”並在cmd中寫入“npm i”
這應該在 package.json 的腳本中
"開始":"節點 index.js",
我不是在談論 nodemon ,在這里使用簡單的 node
可能是版本造成了問題,您可以通過將所有“devDependencies”復制到“package.json”中的“dependencies”並在這樣的版本中放置“*”來避免它
“依賴關系”:{
"cors": "*",
“表達”: ”*”,
"nodemon": "*",
“socket.io”:“*”
},
“devDependencies”:{}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.