Error: Cannot find module 'socket.io'

前端 未结 6 738
闹比i
闹比i 2020-12-16 10:02
[~]# node node.js

Error: Cannot find module \'socket.io\'

[~]# node -v
v0.10.10

socket.io installed:

npm install socket.io

npm W         


        
相关标签:
6条回答
  • 2020-12-16 10:34

    you might have installed but not added to the dependencies in package.json Use below command to install socket.io module

    npm install socket.io --save
    

    Hope this will resolve your problem..

    0 讨论(0)
  • 2020-12-16 10:37

    I had the same issue with version 0.12.0 on Windows. I tried npm install -g socket.io but that didn't change anything. Also tried npm cache clean also no change, but after npm update npm -g, things got well.

    0 讨论(0)
  • 2020-12-16 10:38

    Looks like you have installed socket.io in a different location to your current path. Either install globally like below:

    npm install -g socket.io

    Or reference the location you've installed to:

    var io = require('../lib/socket.io');
    
    0 讨论(0)
  • 2020-12-16 10:39

    Thanks ajtrichards!

    Just to add to the answer - in case you simple use

    sudo npm install socket.io
    

    The installation path will be

    /home/.../.npm/socket.io
    

    If you use sudo npm install -g socket.io

    The installation path will be

    /usr/local/lib/node_modules/socket.io
    

    In first case, I tried adding the socket.io path in global path variable but it did not work.

    0 讨论(0)
  • 2020-12-16 10:43

    I think that you have executed the command npm install socket.io in a different location and your files are in different directory.. So either run the command in the same directory which have your files or either mention the path where you have currently installed socket.io in your PATH variable.

    0 讨论(0)
  • 2020-12-16 10:46

    This almost happens than you try to get socket.io in you html files like :

    index.html

    where you have:

     < script type="text/javascript" src="/socket.io/socket.io.js"></script>
    

    It will not find socket.io because you did not started module in you application file wich contain the server like

    server.js

    You must include following lines after started your server in server.js :

    var io = require('socket.io').listen(server);
    

    Hope, will save time.

    0 讨论(0)
提交回复
热议问题