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

205
Vistas
nodejs import dynamic variable from another file

by using module.exports = var; and const var = require("./file.js"); we can access a variable from another file but the imported variable is static and cannot change even if the original variable changes in the original file, how can I export an array that can be updated at any time and accessible in real time in another file?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you are storing the variable somewhere in the code by which you can differentiate between the files that you're interested in reading.

You might use the following approach by which you change the file name dynamically with the variable.

const readFile = async() => {
    try {
        const num = 1;
        // your variable which will be used to change the destination file. 
        let world = await fs.promises.readFile(path.join(__dirname, `data/fileName${num}.json`), "utf8");
        world = JSON.parse(world);
        console.log(world);
        // data you're intrested in! 
    } catch (err) {
      console.log(err);
    }
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

put your variable inside of a function that returns the variable then export the function

export function getVariable(){
  let myVar = 0;
  return myVar
}
module.exports = getVariable;

const getVar = require('../file.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