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"
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")