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

188
Visualizações
How can I dynamically change an object that is set in another file?

I have a problem and need your help. I am currently working on a project in which I have to save a javascript object in an external file and export the whole thing via module.exports.

Now to my problem and I would now like that another file changes a certain value in my object. This is also possible with:

OBJECT.VALUE = "YOUR NEW VALUE"

Problem is that it now works in this session but the object should permanently change this value means Node.js now has to change this value in the object file too.

I guess I have to use the fs library but I don't know how exactly? I would like to keep the structure of the object means the script should only change the value

Here you have the Object File:

const Core = new Object();

Core['config'] = {

    ['test'] : "Test"

}

// Export the Core
module.exports.Core = Core;

What i tried in other File:

const { Core } = require('./settings/Core.js')


Core['config']['test'] = "MY NEW VALUE"
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are several ways you could go about this.

Create a getter and setter function in your object file and export them.

function setObject(field, newVal){ 
  Core.config[field] = newVal
}

function getObject(){
  return Core;
}


module.exports = {setObject, getObject}

In your other file call the function.

const {setObject, getObject } = require('./settings/Core.js')

Now just use the functions to retrieve or update the object

setObject("Test", "NewValue")
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