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

109
Vistas
Changing same instance of variable from other file nodejs

I'm not sure how to formulate an explanation, but I want to know how I can change a variable from the index.js file in another file. I thought by doing module.exports=(thing you want to export) then requiring the file will allow me to access the same instance of that variable, but it seems like it returns undefined. Bare in mind, I'm new to nodejs and I want to learn.

in index.js:

const multiVerse={stuff};
module.exports=multiVerse;

in other js file I want to use this in:

const main = require(./index.js)
//change data of object
main.multiVerse;

question: how can I access a variable from the index file in some other place in the project?

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

0

const multiVerse = {foo: 'bar'};
module.exports = multiVerse;

// ...

const main = require('./index.js');
main.foo = 'baz';

or

const multiVerse = {foo: 'bar'};
module.exports = { multiVerse };

// ...

const main = require('./index.js');
main.multiVerse.foo = 'baz';

or

const multiVerse = {foo: 'bar'};
module.exports = { multiVerse };

// ...

const { multiVerse } = require('./index.js');
multiVerse.foo = 'baz';
about 4 years ago · Juan Pablo Isaza Denunciar

0

you can export like this -

in index.js

    exports.variableA = 'some_value';

in other.js

    var indexExports = require('./index');
    ...
    ...
    indexExports.variableA = 'Any_value';
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