Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

368
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda