1

I was reading this Node.js In Action and there's this index.html which references socket.io.js from node_modules folder (I believe).

How does the following script tag resolves the path to the socket.io.js when the node_modules folder is one directory up from where the index.html resides?

The script tag:

<script src="/socket.io/socket.io.js" type="text/javascript"></script>
try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
Samot
  • 11
  • 2
  • Possible duplicate of [Can't find socket.io.js](http://stackoverflow.com/questions/8689877/cant-find-socket-io-js) – omarjmh May 15 '16 at 03:19
  • Thanks JordanHendrix but in my case, there is no error saying the file cannot find. From my end, everything works but I want to understand how the script tag managed to locate the socket.io.js inside the node_modules/socket.io directory when its not explicitly specified in the script tag. Thanks a lot in advance! – Samot May 15 '16 at 03:24

2 Answers2

1

When you include a module in your file, node first looks for the module in node_modules folder in current directory. If it does not find it there then it moves to parent directory and looks for the module in node_modules present in that directory, and it keeps moving up until it finds the required module.

Vikash Kesarwani
  • 850
  • 5
  • 14
0
  • Socket.io reads the content of the socket.io-client/socket.io.js file into a variable.

  • Socket.io has access to your app's http server, it then listens to requests that would match the URL of something like '/socket.io/socket.io.js' and responds with the contents of that variable above

You can see this in the socket.io source code:

Saeed Jahed
  • 821
  • 5
  • 8
  • Thanks Saeed, so are you saying that '/socket.io/socket.io.js' matches the actual directory of where the socket.io.js really resides (i.e., node_modules/socket.io/lib/socket.io.js)? Thanks a lot! – Samot May 15 '16 at 03:30
  • 1
    @Samot, No the physical file path and the URL do not need to match. The physical file comes from another npm package called socket.io-client which socket.io package depends on. So the file is located under `node_modules/socket.io-client/socket.io.js` – Saeed Jahed May 15 '16 at 03:35
  • Ok Thanks Saeed. So is it safe and correct to say that the '/socket.io/socket.io.js' refers to 'node_modules/socket.io/lib/socket.io.js' then? Thanks in advance – Samot May 15 '16 at 03:38
  • "So the file is located under node_modules/socket.io-client/socket.io.js " - I can't find this directory though...Is it under my project directory? Thanks! – Samot May 15 '16 at 03:40
  • It's not entirely safe. While it is currently served from this location, in the future socket.io might move it to a different location but still serve it from the same URL. If you let me know what you're trying to achieve, I might be able to help you better. – Saeed Jahed May 15 '16 at 03:40
  • Under my project directory, all i can find is node_modules/mime and node_modules/socket.io – Samot May 15 '16 at 03:41
  • If you have an older version of NPM, it might be under `node_modules/socket.io/node_modules/socket.io-client`. – Saeed Jahed May 15 '16 at 03:42
  • Thanks a lot Saeed. Thanks for your prompt responses! I appreciate it. – Samot May 15 '16 at 04:04