Timmiej93
Timmiej93

Reputation: 1427

Why do I keep getting "Cannot find module 'socket.io'"?

I have been trying to use socket.io in my NodeJS script, but I'm constantly getting the error "Cannot find module 'socket.io'".

Full error:

$ sudo node /var/www/apache/server/serverScript.js
module.js:549
    throw err;
    ^

Error: Cannot find module 'socket.io'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/var/www/apache/server/serverScript.js:59:12)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

I've tried installing socket.io globally, I've updated everything npm related, and tried all other common suggestions I found online, but the error hasn't changed.

If I had to guess, I think it's related to the package.json file. I started off with NodeJS just recently, and never used a package.json file. I saw somebody mention it as a fix, so I added one through npm init. This placed it in /home/pi. Since this didn't help, I moved the file to the js file location: /var/www/apache/server. Unfortunately, no luck.

Can anyone tell me what's causing this issue?

Upvotes: 1

Views: 6523

Answers (4)

Nick N
Nick N

Reputation: 1048

My issue was in a docker-compose file where I wasn't doing an npm install during the command function.

I fixed it by adding changing in the compose file from:

command: npm run dev

to:

command: bash -c "npm install && npm run dev"

Upvotes: 0

acarlstein
acarlstein

Reputation: 1838

If you are developing in NodeJS, you need to add the package library to your package.json. This can be done by doing:

npm install --save socket.io

Note: If your npm install fails to install socket.io, try to:

  • Install node-gyp globally: npm install -g node-gyp
  • Create a .npmrc file with this in the content: @types:registry=https://registry.npmjs.org/

which is going to add this line to your package.json:

"dependencies": {
    "debug": "~3.1.0",
    ...    
    "socket.io-adapter": "~1.1.0",
    "socket.io-client": "2.1.1",
    "socket.io-parser": "~3.2.0"
},

Note: the dots are other packages library that may exists in there.

Then, you can use it in your code:

const io = require('socket.io');

Upvotes: 0

Kim Nedergaard Clausen
Kim Nedergaard Clausen

Reputation: 232

It's hard to give a good answer without any of your source code. However, you can try do to this.

  1. Open your cmd or terminal.
  2. cd into your project folder.
  3. Run the command: npm i --save socket.io
  4. Within your source code, type const io = require('socket.io'); to import socket.io.

Upvotes: 7

Tahar Chibane
Tahar Chibane

Reputation: 141

Have you place this code at the bottom of your render document (like index.html for example) ?

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
</script>

You can just install socket IO inside your project not globally.

Upvotes: 0

Related Questions