Socket.IO - require is not defined

后端 未结 8 2160
Happy的楠姐
Happy的楠姐 2020-12-28 08:49

I\'m trying to get socket.io working but now in Chrome I get the error:

Uncaught ReferenceError: require is not defined

client.php:9

相关标签:
8条回答
  • 2020-12-28 09:24

    Actually I was successful in getting Apache to serve up these files, today in fact.

    Simply create a copy (I actually copied the files, soft links might be ok I did not try) of the contents of node_modules/socket.io/node_modules/socket.io-client/dist to your web root. For my install of socket.io (0.9.16) where I installed socket.io globally these two commands did the trick:

    sudo cp -av /usr/lib/node_modules/socket.io/node_modules/socket.io-client/dist /var/www/socket.io 
    chown -R www-data:www-data /var/www/socket.io
    

    This was on a Ubuntu 13.10 box the usual LAMP stack installed before I then added node.js.

    Now keeping this copy in sync as new versions of socket.io are released is a problem I have not yet tried to address yet.

    Cheers

    0 讨论(0)
  • 2020-12-28 09:25

    As others have noted, when io is run on the server side, an "automagical" service of "socket.io.js" is served - and it branches out to the other js files in its lib directory.

    What I wish to add, is that This is Not Intutive and, no, you cannot have your regular webserver serve this file - the errors just get more strange and seemingly contradictory (require not defined, etc). This is not a normal js file, and I wish all the tutorials out there would emphasize that.

    Anyhow, if you are running behind nginx, for example, set the port-settings to the same port assigned for node - port 3000 in my case. I used that port in the client like this:

      <script src="http://localhost:3000/socket.io/socket.io.js"></script>
    

    ... and then also used port 3000 in the server code - there are too many ways / versions you can code the server-js-code for an example to do anything but confuse most who are using 'a different way'.

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