已安装 node.js websocket 模块但无法在脚本中工作 [英] node.js websocket module installed but won't work in scripts

查看:36
本文介绍了已安装 node.js websocket 模块但无法在脚本中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了 node.js + microsoft visual 才能安装 websocket,它安装得很好:

I just installed node.js + microsoft visual to be able to install the websocket, it installed fine:

C:\Users\Administrator>npm install websocket
npm http GET https://registry.npmjs.org/websocket
npm http 304 https://registry.npmjs.org/websocket

> websocket@1.0.8 install C:\Users\Administrator\node_modules\websocket
> node install.js

[websocket v1.0.8] Attempting to compile native extensions.
[websocket v1.0.8] Native extension compilation successful!
websocket@1.0.8 node_modules\websocket

C:\Users\Administrator>

现在我正在尝试运行一个脚本:

Now i'm trying to run a script with this:

   var WebSocketServer = require('websocket').Server;

我明白了:

C:\Users\Administrator>node C:\server\src\main.js

module.js:340
throw err;
      ^
Error: Cannot find module 'websocket'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (C:\server\src\main.js:2:23)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)

C:\Users\Administrator>

任何帮助将不胜感激,我已经尝试安装多次,我尝试全局安装(-g 标记).

Any help would be appreciated, I already tried installing several times, I tried installing it globally (-g tag).

推荐答案

请注意,如果您全局安装了 NPM 模块(使用 -g 选项)并且没有告诉 node 使用全局模块路径来解决需求.

Note that you can get this error if you installed an NPM module globally (with the -g option) and have not told node to use the global module path to resolve requirements.

在 Linux 上,我全局安装了 websocket 模块:

On Linux I installed the websocket module globally:

$ sudo npm install -g websocket
npm http GET https://registry.npmjs.org/websocket
npm http 200 https://registry.npmjs.org/websocket
npm http GET https://registry.npmjs.org/websocket/-/websocket-1.0.8.tgz
npm http 200 https://registry.npmjs.org/websocket/-/websocket-1.0.8.tgz

> websocket@1.0.8 install /usr/local/lib/node_modules/websocket
> node install.js

[websocket v1.0.8] Attempting to compile native extensions.
[websocket v1.0.8] Native extension compilation successful!
websocket@1.0.8 /usr/local/lib/node_modules/websocket

在此之后,我必须导出 NODE_PATH 环境变量以指向上述输出中提到的路径:

Following this I had to export the NODE_PATH environment variable to point to the path mentioned in the above output:

export NODE_PATH=/usr/local/lib/node_modules

之后:

$ node
> require('websocket')
{ server: 
   { [Function: WebSocketServer]
     super_: { [Function: EventEmitter] listenerCount: [Function] } },
     ...

希望这有助于搜索此错误消息的人.

Hopefully this helps someone searching for this error message out.

这篇关于已安装 node.js websocket 模块但无法在脚本中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆