I have code that reference a local library, which has an environments file.
The folder structure looks like
> common-lib
-src
--Env
----index.js
--Services.js
----index.js
--index.js
> myapp
-src
--index.js
/Env/index.js:
export const SECRET_CREDENTIALS = {
mykey: `IT'S THE KEY`
}
/Services/index.js:
import { SECRET_CREDENTIALS } from ".."
const KEY = SECRET_CREDENTIALS.mykey
export function showVar(orderCart) {
return KEY
}
index.js:
export * from './Services'
export * from './Env'
Now, when I call showVar from the library in myapp, at index.js, I get an error saying cannot access SECRET_CREDENTIALS before it's initialized. The error is avoided if I move the const KEY = SECRET_CREDENTIALS.messaging into the function showVar itself.
Why is this happening, and how should I structure things properly so that the initialization order is correct?
Because the export in the common-lib index.js exports the showVar first, before it exports SECRET_CREDENTIALS, and showVar is importing SECRET_CREDENTIALS from the library's index.js, it would be importing before the var is available.
Either, import directly from ../Env, or move the order of the exports in common-lib's 'index.js'