Socket.io error

I am trying to start Nodejs using the Socket.io module. I installed the latest version of Nodejs and I installed socket.io from a command prompt that is open as an administrator (I'm on Windows 7) using the npm install socket.io command. The installation completes without any problems, but when I try to run the following test program:

var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); 

I get this error:

 module.js:340 throw err; Error: Cannot find module 'socket.io' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:362:17) at require (module.js:378:17) at Object.<anonymous> (C:\xampp\htdocks\HTML5Game\impact\app.js:1:72) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) at Function .Module._load (module.js:312:12) at Module.runMain (module.js:487:10) 

In my search I found some things about dependency issues and some incompatibility suggestions between socket.io and Nodejs version, but both talked about older versions of socket.io and Node Thanks.

+7
source share
7 answers
 cd app rm -rf node_modules npm cache clean npm install 

Explanation

 cd app 

Go to the application directory

 rm -rf node_modules 

Remove installed modules.

 npm cache clean 

Delete the npm cache (some errors are caused by this)

 npm install 

Install the modules listed in package.json . If you do not have package.json , you can install a module like this

 npm install <module_name> 

Example

 npm install socket.io 

In your case, if you don't know what package.json , read here here before continuing with your work on nodejs.

+27
source

I had to solve this problem as follows.

1) I put the test program in the following folder.

c: \ program files \ nodejs \ node_modules

In this case, the source code is as follows.

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

2) I indicate the full path to socket.io.

c:> dir / x [enter]

Then I type the following command to get "progra ~ 1".

 var io = require("c:/progra~1/nodejs/node_modules/socket.io").listen(80); 
+5
source

If you want to use the module in more than one project or have a clean project directory, you can add the -g option to the npm command. For example:

 npm install socket.io -g 
+4
source

I had the same problem. The only thing you need to do is run "npm install socket.io" not in the folder where you installed your node.js, but in the folder where you launched the node server file. For example, I have a server.js file with the code `

 var io = require('socket.io').listen( io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); 

`only in the same folder run 'sudo npm install socket.io' and everything should be fine to go.

+3
source

In the future, for those who are wondering what the real problem is, there is this two year error with npm that has not yet been resolved: https://github.com/isaacs/npm/issues/1341

The problem is that if you have a socket.io dependency already installed in your top-level directory node_modules, then npm will not install this dependency for any modules you install that depend on it.

All you really need to do when you encounter difficulties is the following (replace socket.io with any module that gives you problems):

 mv node_modules node_modules.bak npm install socket.io mv node_modules/socket.io node_modules.bak rmdir node_modules mv node_modules.bak node_modules 
+2
source

Unable to find the module "socket.io" means that you do not have the module "socket.io" installed in your node modules.

just run the following command:

 npm install socket.io 

if you run:

 npm install socket.io --save 

it will update the package.json file.

+1
source

I fix it with

 npm install --save socket.io 

And it works!

How do they do it at http://socket.io/get-started/chat/

0
source

All Articles