Node.js已安装 Websocket 模块,但在脚本中不起作用



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

C:UsersAdministrator>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:UsersAdministratornode_moduleswebsocket
> 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_moduleswebsocket
C:UsersAdministrator>

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

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

我得到的是:

C:UsersAdministrator>node C:serversrcmain.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:serversrcmain.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:UsersAdministrator>

任何帮助都将不胜感激,我已经尝试安装了好几次,我尝试在全球范围内安装它(-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] } },
     ...

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

我在这里得到了相同的错误。。我遵循以下步骤:

以管理员身份执行cmd(右键单击cmd图标->以管理员身份运行(然后键入cmd:

c:Node Instalation Dir> npm install -g express
c:Node Instalation Dir> npm install websocket --force

现在你可以运行你的脚本:

c:Node Instalation Dir> node script.js

我在测试服务器上做了。。尽量不要在生产服务器中使用"--force",好吗?

npm将模块安装在./node_modules/中。因此,您应该在C:serversrc中安装websocketIN

最新更新