Reputation: 979
I am attempting to run Nodejs with the Socket.io module. I have installed the latest version of Nodejs, and I installed socket.io from a command prompt opened as an administrator (I'm in windows 7) using the command npm install socket.io The install appears to complete without any problems but when I attempt 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 receive 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 searching I found some things about dependency problems, and some suggestions about incompatibility between socket.io and a version of Nodejs, but both were talking about older versions of socket.io and Node Thanks.
Upvotes: 7
Views: 27618
Reputation: 21
Cannot find module 'socket.io' means you don't have 'socket.io' module installed in your node modules.
simply run the following command:
npm install socket.io
if you run:
npm install socket.io --save
it will update your package.json file.
Upvotes: 1
Reputation: 517
I fix it with:
npm install --save socket.io
And it run!
That's how they do it in http://socket.io/get-started/chat/
Upvotes: 0
Reputation: 10241
For future reference, for those wondering what the real problem is, there is this two year old bug with npm that has yet to be addressed: https://github.com/isaacs/npm/issues/1341
The problem is that if you have a dependency of socket.io already installed in your top-level node_modules directory then npm won't install that dependency for any modules you install that depend on it.
All you really have to do when you run into trouble is the following (replace socket.io
with whatever module is giving you trouble):
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
Upvotes: 2
Reputation: 474
If you want use a module in more then one project or have clean project directory you can add "-g" parameter to npm command. Like so:
npm install socket.io -g
Upvotes: 4
Reputation: 131
I've had the same problem. The only thing you need to do is to run "npm install socket.io" not in the folder where you have installed your node.js, but in the folder where you started your node server file. For example I have file server.js with 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);
});
});
` just in the same folder run 'sudo npm install socket.io' and everything should be good to go.
Upvotes: 3
Reputation: 51
I had to work around this problem in the following way.
1) I put a 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 specify the full path of the socket.io.
c:>dir /x[enter]
I then type the following command to get the "progra~1".
var io = require("c:/progra~1/nodejs/node_modules/socket.io").listen(80);
Upvotes: 5
Reputation: 3025
cd app
rm -rf node_modules
npm cache clean
npm install
cd app
Go to your app directory
rm -rf node_modules
Delete your currently installed modules
npm cache clean
Delete your npm cache, (some errors are caused by this)
npm install
Install modules listed in your package.json
. If you don't have a package.json
, you can install a specific module like this
npm install <module_name>
Example
npm install socket.io
In your case, if you don't know what a package.json
is, please read up on it here before continuing to work on nodejs.
Upvotes: 27