Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

367
Views
Cannot access before initialization - defining ENV vars across multiple files

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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'

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!