gpt4 book ai didi

node.js websocket 模块已安装但在脚本中不起作用

转载 作者:IT老高 更新时间:2023-10-28 23:24:48 25 4
gpt4 key购买 nike

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

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>

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

   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 标签)。

最佳答案

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

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

$ 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 环境变量以指向上述输出中提到的路径:

export NODE_PATH=/usr/local/lib/node_modules

之后:

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

希望这可以帮助搜索此错误消息的人。

关于node.js websocket 模块已安装但在脚本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14152635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com