最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js, getting cannot find module 'ws' error - Stack Overflow

programmeradmin2浏览0评论

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'
Share Improve this question edited Jul 16, 2022 at 6:52 Youssouf Oumar 45.9k16 gold badges100 silver badges104 bronze badges asked Nov 26, 2014 at 7:37 KatochKatoch 2,77710 gold badges57 silver badges88 bronze badges 2
  • 4 Run the npm install command inside of your script folder. Also, I think it should be "npm install ws". – Jonathan Gray Commented Nov 26, 2014 at 7: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 Commented Nov 26, 2014 at 8:01
Add a comment  | 

4 Answers 4

Reset to default 28

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.

  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.

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:

  • 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');

发布评论

评论列表(0)

  1. 暂无评论