Node.js websocket module installed, but will not work in scripts

I just installed node.js + microsoft visual to be able to install websocket, it installed perfectly:

C:\Users\Administrator>npm install websocket npm http GET https://registry.npmjs.org/websocket npm http 304 https://registry.npmjs.org/websocket > websocket@1.0.8 install C:\Users\Administrator\node_modules\websocket > node install.js [websocket v1.0.8] Attempting to compile native extensions. [websocket v1.0.8] Native extension compilation successful! websocket@1.0.8 node_modules\websocket C:\Users\Administrator> 

Now I am trying to run a script using this:

  var WebSocketServer = require('websocket').Server; 

and I get this:

 C:\Users\Administrator>node C:\server\src\main.js module.js:340 throw err; ^ Error: Cannot find module 'websocket' 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:\server\src\main.js:2:23) 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:492:10) C:\Users\Administrator> 

Any help would be appreciated, I tried several times to install it, I tried to install it globally (-g tag).

+7
source share
3 answers

Note that you can get this error if you installed the NPM module globally (with the -g option) and did not tell node use the global module path to resolve the requirements.

On Linux, I installed the websocket module globally:

 $ sudo npm install -g websocket npm http GET https://registry.npmjs.org/websocket npm http 200 https://registry.npmjs.org/websocket npm http GET https://registry.npmjs.org/websocket/-/websocket-1.0.8.tgz npm http 200 https://registry.npmjs.org/websocket/-/websocket-1.0.8.tgz > websocket@1.0.8 install /usr/local/lib/node_modules/websocket > node install.js [websocket v1.0.8] Attempting to compile native extensions. [websocket v1.0.8] Native extension compilation successful! websocket@1.0.8 /usr/local/lib/node_modules/websocket 

After that, I had to export the NODE_PATH environment variable to point to the path specified in the above output:

 export NODE_PATH=/usr/local/lib/node_modules 

After that:

 $ node > require('websocket') { server: { [Function: WebSocketServer] super_: { [Function: EventEmitter] listenerCount: [Function] } }, ... 

Hope this helps someone find this error message.

+4
source

I have the same error here. I am doing the following steps:

Run cmd as administrator (right-click the cmd icon-> Run as administrator) Then enter cmd:

 c:\Node Instalation Dir\> npm install -g express c:\Node Instalation Dir\> npm install websocket --force 

Now you can run the script:

 c:\Node Instalation Dir\> node script.js 

I did on a test server. Do not try to use '-force' on a production server?

+3
source

npm install the module in ./node_modules/ . Therefore, you must install websocket IN C:\server\src\ .

0
source

All Articles