16

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'
Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
Katoch
  • 2,709
  • 9
  • 51
  • 84
  • 4
    Run the npm install command inside of your script folder. Also, I think it should be "npm install ws". – Jonathan Gray Nov 26 '14 at 07:53
  • Also you need to add the node installation directory to your global environment variable "PATH" in order to use the command as-is when the command-line path is different than that directory. Unless you do that you need to specify the full working path to the npm executable when you run the command outside of the directory ex. c:\myscript> c:\nodeinstall\npm.exe install ws – Jonathan Gray Nov 26 '14 at 08:01

4 Answers4

27

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.)

Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
Zlatko
  • 18,936
  • 14
  • 70
  • 123
4

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.

Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
Jonast92
  • 4,964
  • 1
  • 18
  • 32
0
  1. Go to your project root directory and open the package.json file and edit
  2. Add "type":"module";
  3. Go to the top of your js file, delete the require and use the import statement to import 'ws' into your script.
Ene Ikenna
  • 47
  • 1
-1

I was also facing the same issue.

Error 1:

enter image description here

Just simply trying npm install ws to the same folder level where I had my server.js worked for me.

enter image description here

Error 2:

  • If you are getting the error 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');

enter image description here

enter image description here

KushalSeth
  • 3,265
  • 1
  • 26
  • 29