简体   繁体   中英

How does script tag resolves path to node_modules?

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>
  • 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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM