0

For example:

File 1:

const env = process.env.VARIABLE;
export const config: any = config[env.toLowerCase()];

File 2:

import { config } from '../file1';
class SomeClass {
  constructor() {
    console.log(config.someProperty);
  }
}
export default new SomeClass();

This doesn't seem to work. config.someProperty is undefined. Any reason why?

Edit per request:

import { config } from '../file1';
export class SomeClass {
  private myConfig = config.someProperty;
  constructor() {
    console.log(myConfig);
  }
}
cyberguy
  • 233
  • 3
  • 10
  • Because `config` is a string. Not sure what properties you expect it to have? – Bergi Nov 02 '20 at 16:23
  • While your problem is actually unrelated to the `export default new ClassName()`, notice [one should not use that](https://stackoverflow.com/q/39076190/1048572). – Bergi Nov 02 '20 at 16:25
  • 1
    Wait, no, `env` is a string (weird name choice), and `config` is coming from a circular definition that throws an exception when evaluating file1. – Bergi Nov 02 '20 at 16:29
  • So circular reference, ok. I am using the default export new pattern because I want it to be a singleton. I was told that using the pattern of having a private instance, and a static method getInstance() would not guarantee a singleton. – cyberguy Nov 02 '20 at 16:39
  • Yes, `const config = config[env.toLowerCase()];` can't work, on its own already, regardless of any exports/imports. It's in the temporal dead zone. What did you attempt to do there, did you mean to refer to something else? – Bergi Nov 02 '20 at 16:45
  • Neither does `export default new ClassName` guarantee a single instance of `ClassName`. – Bergi Nov 02 '20 at 16:46
  • `const config = config[...]` does work though (I am able to import and use it in other files). Just not in any classes that are using `export default new`. Do you happen to have any resources other than the stack overflow answer above that describes how to implement a singleton pattern in node? – cyberguy Nov 02 '20 at 17:02
  • No, it certainly doesn't. Not in any standard-compliant implementation of JavaScript. What do you think the `config` on the right hand side refers to? What does your actual code look like that you say works? – Bergi Nov 02 '20 at 17:11
  • Regarding your singleton, just don't use a `class` and you'll be good. See the linked answer. Btw, the problem with the `config` is not the export, it's the immediate usage. Try a `console.log(config.someProperty)` in the top-level scope and it won't work either. – Bergi Nov 02 '20 at 17:14
  • I updated the question with an example of how it is working in my app. I'm using webpack if that makes any difference, but it certainly does work. – cyberguy Nov 02 '20 at 17:29
  • I meant the actual code that declares and initialises `config`. (Yes, webpack might suppress the TDZ error, but it should still throw an exception when accessing `config[env.toLowerCase()]` on `undefined`) – Bergi Nov 02 '20 at 17:33

1 Answers1

0

The answer is to ensure there are no circular dependencies if your environment isn't equipped to handle them. For me, I had a circular dependency between the two classes (not shown in the question), which was resolved by moving the config setup to its own module.

cyberguy
  • 233
  • 3
  • 10