This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

Join us in building a kind, collaborative learning community via our updated
Code of Conduct.

I'm not convinced about the global scope argument. After all, NPM implies a global namespace anyway. It's certainly a trade-off to consider as there are clear downsides to the in-your-face interruption of business/application logic to perform an infrastructure-level require statement.
– mahemoffAug 31 '11 at 21:23

@mahemoff you should read up on global scope. It's clearly evil for the normal reasons.
– RaynosAug 31 '11 at 21:50

3 Answers
3

Note that coffee-script isn't a valid identifier, so your code isn't really importing it properly. You can use CoffeeScript's flexible object literals to handle this pretty nicely. I'd also use ?= to avoid unnecessarily re-importing modules. Building off of user211399's answer:

Since I'm allowing you to import with different identifiers in different modules, using the global namespace feels particularly unsafe. I'd import them locally instead, as shown below. Be aware that because this uses eval it might not fail gracefully if you specify an illegal identifier.

it is a valid identifier. You just need to refer to it as global["coffee-script"]
– RaynosAug 31 '11 at 22:23

2

@Raynos: You can use that as a reference to it, but that's not what it means to be an identifier (ECMA-262 5e Section 7.6, Section 10.3.1, Section 11.1.2). That's also not how you'd refer to it if you'd imported it normally, which is why I don't consider it to be "really importing it properly".
– JeremyAug 31 '11 at 22:28

Ended up scratching it and just doing it the usual way - it ends up being a bigger hassle. There are plenty of times where you need to grab a module's property or use a different naming scheme. Also, assigning to global scope is not the same as a "normal" require. Aligning assignments makes it easier to read: