3 ms·
FWIW, JS does have namespaces: directories. There's no big semantical difference between var Hello = require("someorg/hey"); and in eg C#: using Hel
by skrebbel 7y ago
FWIW, JS does have namespaces: directories.
There's no big semantical difference between
var Hello = require("someorg/hey");
and in eg C#:
using Hello = SomeOrg.Hey;
The only difference is that in JS you're forced you choose a name for the variable, whereas in C# (and many other languages) you typically just give it the default name (`using SomeOrg.Hey;`).
In short, the node_modules package resolution algorithm is how JS does namespaces. It's the same thing, just with different syntax (and, IMO, a bit more weirdness). The fundamental thing isn't "no namespaces" but "namespaces are package-relative". I wonder whether they got that right through sheer genius, or by accident.
(I recognize that I'm being super pedantic here btw, hope that's OK)
Note that I agree with the rest of your comment, NPM is splendid and it's impressive how much they got right at the first go, especially given how NodeJS was on a hype train at the time (many things on hype trains only get tooling like this right after the first so many tries).
- greggman3 7y agothat might be true in c# but it's not true in C++ iirc. the library declares its "global" namespace which dicates what global symbol will be generated for the linker. "using" creates an alias for that symbol in consumers of the library but nothing you can do short of changing the source of the library will fix the issue of 2 libraries using the same namespace. This is not true for JS Update : Verified the same issue exists in c#.
- lioeters 7y ago> ..The node_modules package resolution algorithm is how JS does namespaces.. I wonder whether they got that right through sheer genius, or by accident. I recall hearing Ryan Dahl's regrets about design decisions in Node.js, a few of which had to do with the module/package resolution algorithm. 10 Things I Regret about Node.js - https://www.youtube.com/watch?v=M3BM9TB-8yA https://www.youtube.com/watch?v=M3BM9TB-8yA There are some good aspects of it though. I like how modules/packages are just imported objects (the path resolution is a bit weird and seemingly inefficient, i.e., checking every parent folder's node_modules folder). It goes together well, that modules are also self-contained function scopes by default (I suppose a kind of anonymous namespace), that exports a plain object with assigned properties. --- Wow, I just learned about C++20 Modules, looks like a game-changing feature. https://modernescpp.com/index.php/c-20-modules https://modernescpp.com/index.php/c-20-modules
- skrebbel 7y agoYeah I saw that talk too, and I was baffled. He seems totally ready to throw out the baby with the bathwater. Node's package resolution algorithm might not be perfect, but it's better than nearly every other language's (which is "single big unhierarchical dump of packages, good look sorting out the dependencies kthxbye").