Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js script -- websocket error

I have installed node.js on my windows-7 PC. I am not able to create websocket connection to a remote server.

I tried to load modeule "ws" in my script :--

var WebSocket = require('ws')

It gave an error :--

cannot find module 'ws'

So i followed instructions over here :--
node.js websocket module installed but won't work in scripts

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

Then run my script :--
D:\My Script Folder \> node myscript.js

Again same error. What could be the problem ?

cannot find module 'ws'
like image 958
Katoch Avatar asked Nov 26 '14 07:11

Katoch


People also ask

How do I fix WebSocket connection error?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

Why is my WebSocket connection failing?

Possible explanations: You are trying to reach a plain WebSocket server. The server is not reachable. The client is not compatible with the version of the server.

Is Node JS good for WebSockets?

Node. js can maintain many hundreds of WebSockets connections simultaneously. WebSockets on the server can become complicated as the connection upgrade from HTTP to WebSockets requires handling. This is why developers commonly use a library to manage this for them.

How do I run a node JS WebSocket?

const webSocket = new WebSocket('ws://localhost:443/'); And then we can create an event for the WebSocket to do something when it gets data. That's it we can now receive data from the WebSocket server.


2 Answers

If you install websocket, you should require websocket, not ws:

npm install websocket

Then in REPL:

var websocket = require('websocket');

Alternatively, you can use ws module:

npm install ws

Repl/script:

var ws = require('ws');

Take a look at your node_modules directory (only the first level, the dirs right beneath it). You can require each of them by exact name.

require will actually look for a node_modules in current dir, then if not found, then the parent and again parent etc. If it doesn't find it, it will look for modules installed in global path pointed to by NODE_PATH. (And of course, native modules such as http and net.)

like image 155
Zlatko Avatar answered Oct 04 '22 04:10

Zlatko


Try to install the package locally but not globally, i.e. without the -g option.

Have you installed the module with the -g option? I think that not every module is meant to be installed globally, instead, try installing it locally for the project you are creating (npm install), and check if the error persists. [...]

If you want to just require('something'); it is better to install it locally, otherwise, you have to require('{PREFIX}something'), where prefix is the path to where yo have installed it globally. Check out this blog post , and, as it says, generally the rule of thumb is to install things locally if you are going to use them in your app, and globally if you are going to use them from the command line.

node error cannot find module already installed.

like image 34
Jonast92 Avatar answered Oct 04 '22 05:10

Jonast92