How to modify a js file then save it back to a file?

I like to update a javascript file (module) in node.js then save it. I have the following js module.

var test = {
   scripts: ["a.js","b.js"],
   getScripts: function()
   {
        return this.scripts;
   }
}

module.exports = test;

I like to load the above module in other modules then update scripts array then save it back to a file. I tried to use JSON.stringify(test), but it doesn't preserve getScripts methods.

Is there a hack or trick for this?

You pretty much need to read the script in using readfile, textually manipulate the scripts array, and then write it back out using writefile.