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'
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'
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
.)
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.
I was also facing the same issue.
Error 1:
Just simply trying npm install ws
to the same folder level where I had my server.js worked for me.
Error 2:
WebSocket is not defined
, then try the command npm install ws
, after adding the below code. if ws
doesn't work then try npm install websocket
Also, add this in your file:
const WebSocket = require('ws');
var conn = new WebSocket('ws://localhost:9090');