javascript node.js 脚本——websocket 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27143585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:01:00  来源:igfitidea点击:

node.js script -- websocket error

javascriptnode.js

提问by Katoch

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

我已经在我的 windows-7 PC 上安装了 node.js。我无法创建到远程服务器的 websocket 连接。

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

我试图在我的脚本中加载模块“ws”:--

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

所以我按照这里的说明进行操作:--
安装了 node.js websocket 模块,但无法在脚本中工作

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'

回答by Zlatko

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

如果你安装 websocket,你应该需要 websocket,而不是 ws:

npm install websocket

Then in REPL:

然后在 REPL 中:

var websocket = require('websocket');

Alternatively, you can use wsmodule:

或者,您可以使用ws模块:

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.

看看你的 node_modules 目录(只有第一级,它下面的目录)。您可以通过确切的名称来要求它们中的每一个。

requirewill 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 httpand net.)

require实际上会在当前目录中查找 node_modules,如果没有找到,则是父级和父级等。如果没有找到,它将查找安装在 NODE_PATH 指向的全局路径中的模块。(当然还有本地模块,例如httpnet。)

回答by Jonast92

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

尝试在本地而不是全局安装包,即没有 -g 选项。

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.

您是否使用 -g 选项安装了模块?我认为并不是每个模块都应该全局安装,而是尝试为您正在创建的项目(npm install)在本地安装它,并检查错误是否仍然存在。[...]

如果你只想 require('something'); 最好在本地安装它,否则,您必须 require('{PREFIX}something'),其中前缀是您全局安装它的路径。查看这篇博文,正如它所说的,一般的经验法则是,如果您打算在您的应用程序中使用它们,则在本地安装它们,如果您打算从命令行使用它们,则在全球范围内安装它们。

node error cannot find module already installed.

节点错误找不到已安装的模块