Node.js Module Resolution by visual example

1,579 views

Published on

Node.js has a very clever design for module path resolution and code loading. Most languages punt when asked to load two modules depending on different versions of a shared third module, but not Node. This presentation depicts a handful of module loading scenarios by demonstrating the filesystem layout for each.

Published in: Technology, Art & Photos
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,579
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Node.js Module Resolution by visual example

  1. 1. Altamira Node.js Module Resolution Jeff Kunkle April 19, 2012
  2. 2. Path Resolution
  3. 3. require(“./mymodule.js”); 1
  4. 4. mymodule.js /projects/preso/ main.js require(“./mymodule.js”);
  5. 5. require(“./mymodule”); 2
  6. 6. /projects/preso/ main.js require(“./mymodule”);
  7. 7. mymodule.js /projects/preso/ main.js require(“./mymodule”);
  8. 8. mymodule.js /projects/preso/ mymodule.json main.js require(“./mymodule”);
  9. 9. mymodule.js /projects/preso/ mymodule.json mymodule.node main.js require(“./mymodule”);
  10. 10. require(“./mymodule”); /projects/preso/ main.js mymodule/ package.json init.js
  11. 11. require(“./mymodule”); mymodule/ /projects/preso/ index.js main.js
  12. 12. require(“./mymodule”); mymodule/ /projects/preso/ index.node main.js
  13. 13. require(“mymodule”); 3
  14. 14. require(“mymodule”); node_modules/ /usr/local/node/lib/ index.js mymodule/ * assuming mymodule was a core Node.js module
  15. 15. require(“mymodule”); node_modules/ /projects/preso/ index.js main.js mymodule/
  16. 16. require(“mymodule”); node_modules/ /projects/ index.js main.js mymodule/ preso/
  17. 17. require(“mymodule”); node_modules/ / index.js main.js mymodule/ projects/ preso/
  18. 18. require(“mymodule”); Global load paths also exist, but don’t use them.
  19. 19. Dependency Resolution Magic!
  20. 20. A B C v1.0 v2.0
  21. 21. a/ node_modules/ c/ b/ node_modules/ c/ b.js a.js c.js c.js ./node_modules/ v1.0 v2.0
  22. 22. a/ b/ b.js a.js ./node_modules/ c/ c.js
  23. 23. Jeff Kunkle @kunklejr

×