Because polluting the global namespace is considered a bad thing. You never know if your naming conventions don’t overwrite something else or vice versa.
That’s why basically anything with the capability of importing external modules has namespaces and local scopes - that way you can do anything you want in your file/module and don’t have to worry about breaking things in another file/module (besides the usual ‘Called a method in the wrong way’, of course).
And, yes, it’s slightly annoying to have to reimport stuff in every file. Then again, the alternative is potentially breaking things so there’s that.
In any way, this is a convention designed to “protect” you from yourself (or others). Not in the sense of security but in the sense of more sensible code.
It also makes it possible to easily see where code comes from. “Magic” variables/objects like the one you’d like to create makes it harder for you (or others) to see what’s going on if you revisit your code at a later date.
tl;dr: Don’t. It’s a bad habit and should only be considered if there’s absolutely no other way.
Thank you for the clarification, it makes perfect sense even if it’s annoying .
according to what you explained to me, a question came to my mind:
I’ve noticed that some packages (not sure if all or only some, since I’m still new to meteor) installed from Atmosphere can be used without importing (for instance Momentjs ). does it mean that this package is not using best practices and polluting the global namespace ?
Probably yes. But such packages as they are are probably stemming from legacy conventions before the proper implementation of require / import. You have to remember, Javascript didn’t start out the way the C derivatives did (to name one example).
All the Atmosphere packages I’m using work through import so it’s not as if the times are not changing
Regarding Momentjs - that’s particularly strange because it’s just a repackage of the npm-module which definitely does not work without imports…
In fact, I myself use Moment directly through npm - I try to avoid such repackages because they are dependant on the maintainer keeping up with updates which then sometimes break down.