suppose you did do that, where you have a script that adds a global
logger symbol instead of exporting it from the module like you currently do. now what does it look like when you want to log something?
- you run that script to set up the global logger symbol
- you access that logger symbol directly
you’ve saved all the typing of
const logger = .
but won’t need those requires everywhere, I’m going to require it once in my top level script
– whoever, right after I post this
even then, the cost is that you’re committing to a codebase where “the value of
logger depends on control flow. control flow across files, no less.” it’s more for someone reading code to understand, and it’s more for someone writing code to get right.
one thing I’d look out for is if you ctrl-click
logger (or equivalent in different IDEs), can you see where it’s defined? this works well for
const logger = require(...);. it may be possible to set up some special declarations to make it work with a global symbol too, but would that (1) be worth the effort and (2) add less than one extra line to each file?
you’re asking for opinions, so I’ll leave mine: when it comes to choosing between making the codebase easier to understand and easier to access with various tools vs. saving one require, I’d prefer the former.