Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!