4

I'm pretty new to nodejs and socket.io. At the time I don't know anything about nodejs. I met a horrible error.

My whole steps to install nodejs (on Windows 7):

- Access nodejs.org and click to their green download button.
- Install nodejs in F:/nodejs
- Install socket.io with command npm install socket.io
- Install node supervisor with command npm install supervisor -g

That's all. Then I try to use socket.io with this line:

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

And it output an error:

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:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (F:\nodejs\chat.js:8:10)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

I've tried all suggested method like cleaning cache, reinstalling nodejs or changing socket.io module path. Unfortunately, all of those solutions didn't solve this problem.

This terrible error killed my whole day. I greatly appreciate any help.

2

2 Answers 2

11

Try to execute npm install socket.io in the folder where your node.js file is (where you require the module). npm creates a folder node_modules wherever you execute npm install socket.io and you probably didn't do this in the correct folder.

npm install supervisor -g did work because -g is the global flag and you can basically do that anywhere.

Sign up to request clarification or add additional context in comments.

9 Comments

@pennstatephil yes for supervisor not socket.io though
I did your suggestion. And nothing change.
@user3247703 can you confirm you see node_modules folder in the root folder of your project with socket.io folder init?
I've checked module path with this code console.log(module.paths). And this is the result F:\\nodejs\\node_modules, F:\\node_modules
My socket.io folder was placed in npm app, in windows appData folder
|
2

The problem is that the package installed by npm install socket.io is not only the module itself, but also brings a sample and more stuff.

When you install, npm creates a node_modules folder. Inside that folder, there is a folder named socketio . Inside that one, there is another folder named node_modules . Inside this one, there is a folder named socket.io . Move or copy this one folder (socket.io) to inside the first node_modules folder right where your script is. Et voilla, it works!

folder levels

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.