node.js websocket module installed but won't work in scripts

后端 未结 3 930
慢半拍i
慢半拍i 2021-02-14 22:36

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 G         


        
3条回答
  •  佛祖请我去吃肉
    2021-02-14 23:03

    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.

    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
    

    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
    

    After this:

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

    Hopefully this helps someone searching for this error message out.

提交回复
热议问题