已安装 node.js websocket 模块但无法在脚本中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14152635/
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
node.js websocket module installed but won't work in scripts
提问by SuperBeast
I just installed node.js + microsoft visual to be able to install the websocket, it installed fine:
我刚刚安装了 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
> [email protected] 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!
[email protected] node_modules\websocket
C:\Users\Administrator>
Now i'm trying to run a script with this:
现在我试图用这个运行一个脚本:
var WebSocketServer = require('websocket').Server;
and it I get this:
我明白了:
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>
Any help would be appreciated, I already tried installing several times, I tried installing it globally (-g tag).
任何帮助将不胜感激,我已经尝试安装多次,我尝试全局安装(-g 标记)。
回答by Drew Noakes
Note that you can get this error if you installed an NPM module globally (with the -goption) and have not told nodeto use the global module path to resolve requirements.
请注意,如果您全局安装了 NPM 模块(带有-g选项)并且没有被告知node使用全局模块路径来解决需求,则可能会出现此错误。
On Linux I installed the websocketmodule globally:
在 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
> [email protected] 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!
[email protected] /usr/local/lib/node_modules/websocket
Following this I had to export the NODE_PATHenvironment variable to point to the path mentioned in the above output:
在此之后,我必须导出NODE_PATH环境变量以指向上述输出中提到的路径:
export NODE_PATH=/usr/local/lib/node_modules
After this:
在这之后:
$ node
> require('websocket')
{ server:
{ [Function: WebSocketServer]
super_: { [Function: EventEmitter] listenerCount: [Function] } },
...
Hopefully this helps someone searching for this error message out.
希望这有助于有人搜索此错误消息。
回答by Filipe Tagliacozzi
I′ve got same error here.. I follow these steps:
我在这里遇到了同样的错误..我按照以下步骤操作:
Execute cmd as Administrator (Right click cmd icon-> Run as Administrator) Then type in cmd:
以管理员身份执行cmd(右键cmd图标->以管理员身份运行)然后输入cmd:
c:\Node Instalation Dir\> npm install -g express
c:\Node Instalation Dir\> npm install websocket --force
Now u can run your script:
现在你可以运行你的脚本:
c:\Node Instalation Dir\> node script.js
I did in a test server.. try to dont use '--force' in production server ok?
我在测试服务器上做了.. 尽量不要在生产服务器中使用“--force”好吗?
回答by Ray Shih
The npm install the module in the ./node_modules/. So you should install the websocket INthe C:\server\src\.
npm 将模块安装在./node_modules/. 所以,你应该安装的WebSocket IN的C:\server\src\。

