安装了node.js websocket模块,但不能在脚本中使用

我刚刚安装了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标签)。

我在这里得到了同样的错误。我遵循这些步骤:

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

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

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

 c:\Node Instalation Dir\> node script.js 

我做了一个testing服务器..试图不使用' – 强制'在生产服务器好吗?

请注意,如果您全局安装了NPM模块(使用-g选项)并且未告知node使用全局模块path来解决需求,则可能会出现此错误。

在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环境variables来指向上面输出中提到的path:

 export NODE_PATH=/usr/local/lib/node_modules 

在这之后:

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

希望这可以帮助有人search这个错误信息。

npm将模块安装在./node_modules/ 。 所以你应该在C:\server\src\安装websocket。