Best practice
Don't use global state like env variables in modules
Don't use global state like env variables in modules, only on the very top-level or in one central location. Pass it in the constructor.
This hides dependencies to configuration values and makes them hard to spot.
Bad
class Foo {
doSomething() {
console.log(process.env.MY_ENV);
}
}
new Foo().doSomething();Good
class Foo {
constructor(myEnv) {
this.myEnv = myEnv;
}
doSomething() {
console.log(this.myEnv);
}
}
new Foo(process.env.MY_ENV).doSomething();