40
[~]# node node.js

Error: Cannot find module 'socket.io'

[~]# node -v
v0.10.10

socket.io installed:

npm install socket.io

npm WARN package.json policyfile@0.0.4 No repository field.
npm WARN package.json policyfile@0.0.4 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
robertklep
  • 198,204
  • 35
  • 394
  • 381
DmitriyB
  • 413
  • 1
  • 4
  • 6

9 Answers9

67

Looks like you have installed socket.io in a different location to your current path. Either install globally like below:

npm install -g socket.io

Or reference the location you've installed to:

var io = require('../lib/socket.io');
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
  • 1
    var io = require('/root/node_modules/socket.io/lib/socket.io'); var express = require('/usr/local/lib/node_modules/express/lib/express.js'); – DmitriyB Jun 13 '13 at 17:52
  • @ajtrichards - for me this does not work when I run node from crontab, at least on reboot. If I would add full path to require() function I believe it would work then – Dariux Nov 24 '14 at 09:26
  • dont forget to npm init – Igor L. Jan 24 '17 at 10:10
13

Thanks ajtrichards!

Just to add to the answer - in case you simple use

sudo npm install socket.io

The installation path will be

/home/.../.npm/socket.io

If you use sudo npm install -g socket.io

The installation path will be

/usr/local/lib/node_modules/socket.io

In first case, I tried adding the socket.io path in global path variable but it did not work.

suman1409
  • 186
  • 1
  • 4
6

you might have installed but not added to the dependencies in package.json Use below command to install socket.io module

npm install socket.io --save

Hope this will resolve your problem..

John
  • 181
  • 2
  • 12
4

I had the same issue with version 0.12.0 on Windows. I tried npm install -g socket.io but that didn't change anything. Also tried npm cache clean also no change, but after npm update npm -g, things got well.

Vali S
  • 1,471
  • 2
  • 10
  • 18
4

I had this problem with NodeJs, Typescript and Socket.io 4. the error was:

TS2792: Cannot find module 'socket.io'.

So my fix was to update the tsconfig.json adding a new property (moduleResolution).

tsconfig.json:

{
  "compilerOptions": {
    ....
    "moduleResolution": "node",
  }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
CrgioPeca88
  • 973
  • 7
  • 12
2

This almost happens than you try to get socket.io in you html files like :

index.html

where you have:

 < script type="text/javascript" src="/socket.io/socket.io.js"></script>

It will not find socket.io because you did not started module in you application file wich contain the server like

server.js

You must include following lines after started your server in server.js :

var io = require('socket.io').listen(server);

Hope, will save time.

ales82
  • 21
  • 1
0

I think that you have executed the command npm install socket.io in a different location and your files are in different directory.. So either run the command in the same directory which have your files or either mention the path where you have currently installed socket.io in your PATH variable.

sumitb.mdi
  • 1,010
  • 14
  • 17
  • 1
    Please put some more context into your answer – Alec. Jun 13 '13 at 14:50
  • @AlecHenderson_v1.00 Basically I am saying that; you might have a directory say "ABC" where you have your node.exe files and npm.cmd (if you are using windows operating system) so there you would also be having a node_modules folder so you have to make sure that whenever you install any module you first change directory in cmd and reach there i.e. in "ABC". Also make sure that you have the path of "ABC" mentioned in PATH variable. – sumitb.mdi Jun 13 '13 at 14:57
0

I had the same problem in mac... you can install the module with sudo npm install socket.io-client

Johnstone bos
  • 101
  • 1
  • 3
0

Please check all your js file wherever you mentioned or called socket.io where in in my case it was a spelling mistake sokcet.io that I corrected and start working.

PET_Maya
  • 1
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34341351) – rx2347 May 11 '23 at 17:52