Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require node modules correctly in node.exe of node.js

Recently there is a newer version of node.js which supports npm for windows.

I followed the instructions here And i've installed socket.io successfully on windows through npm (though some failed, like JSDOM)

However when I require the module the followings fails:

var io = require("socket.io").listen(1234); 

and also

var io = require("./lib/node_modules/socket.io/index.js").listen(1234);

Which shows "Cannot find module "socket.io"

However this works:

var io = require('/NodeDev/lib/node_modules/socket.io/index.js').listen(1234);

Here is where my stuff are placed:

folder socket.io : in D:\NodeDev\lib\node_modules\

node.exe : in D:\NodeDev\bin\

my script : in D:\NodeDev\TestProject\

like image 902
Jacky Cheng Avatar asked Mar 07 '26 12:03

Jacky Cheng


1 Answers

I had a similar problem when dealing with generally available packages in Windows. It worked when I installed the packages directly into my project:

cd myProject
npm install socket.io

It then gets installed into the myProject/node_modules folder. At that time, doing a require('socket.io') works for me.

like image 81
Brian Genisio Avatar answered Mar 10 '26 01:03

Brian Genisio