fiction
fiction

Reputation: 1078

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:

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. I reinstalled my modules 5-6 times and recreated the app but it's still giving me the same error. My package.json file has 3 dependencies: Express, Socket.IO and Jade

Upvotes: 20

Views: 8285

Answers (3)

shank_fab_worker
shank_fab_worker

Reputation: 403

I had the same problem , please read it carefully

These are solutions to socket.io related problems

I hope i will work

  1. Ports in your (index.js or server.js) & (index.html and your client.js) port must be different. (refer below code)

=============your index.js file ======================

(port here is 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 ======================

port here is 8080

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

=============your index.html file ======================

port here is 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)

(index.html and your client.js) port must be same

  1. You should always use 'http' while working with socket.io (refer above code)

  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)

  3. Try replacing "io" to "io.connect"

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

  4. Must write tag at the end in the HTML

  5. U may forget to add this code which is must in "socket.io"

It is required in your html file

  1. delete "node_modules" and "package-lock.json" and write "npm i" in cmd

  2. This should be in package.json 's scripts

    "start":"node index.js",

I am not talking about nodemon , use simple node over here

  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

    "dependencies": {

    "cors": "*",

    "express": "*",

    "nodemon": "*",

    "socket.io": "*"

    },

    "devDependencies": {}

Upvotes: 0

Gabriel Oliveira
Gabriel Oliveira

Reputation: 102

In my case I had to add a few modules to dependencies, as they were only under devdependecies and were not built on production

Upvotes: 0

fiction
fiction

Reputation: 1078

Alright so after 2 hours I figured the problem out, multiple folders named "emitter" that contained the indexof module also had a gitignore file that made git ignore the module, no idea why that was even there, but deleting them fixed the problem

Upvotes: 33

Related Questions