Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js websocket module installed but won't work in scripts

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

C:\Users\Administrator>npm install websocket
npm http GET https://registry.npmjs.org/websocket
npm http 304 https://registry.npmjs.org/websocket

> [email protected] 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!
[email protected] node_modules\websocket

C:\Users\Administrator>

Now i'm trying to run a script with this:

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

and it 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 already tried installing several times, I tried installing it globally (-g tag).

like image 400
SuperBeast Avatar asked Jan 04 '13 07:01

SuperBeast


3 Answers

Note that you can get this error if you installed an NPM module globally (with the -g option) and have not told node to use the global module path to resolve 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

> [email protected] 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!
[email protected] /usr/local/lib/node_modules/websocket

Following this I had to export the NODE_PATH environment variable to point to the path mentioned in the above output:

export NODE_PATH=/usr/local/lib/node_modules

After this:

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

Hopefully this helps someone searching for this error message out.

like image 89
Drew Noakes Avatar answered Oct 14 '22 05:10

Drew Noakes


I´ve got same error here.. I follow these steps:

Execute cmd as Administrator (Right click cmd icon-> Run as Administrator) Then type in cmd:

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

Now u can run your script:

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

I did in a test server.. try to dont use '--force' in production server ok?

like image 5
Filipe Tagliacozzi Avatar answered Oct 14 '22 04:10

Filipe Tagliacozzi


The npm install the module in the ./node_modules/. So you should install the websocket IN the C:\server\src\.

like image 1
Ray Shih Avatar answered Oct 14 '22 04:10

Ray Shih