Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda