简体   繁体   中英

Can I change a JSON object that is in module.exports of a .js file programmatically in Node?

I have a .js file with just JSON objects exported. Can I read these in update the objects and write them back to the same file? How? I know how to read a .json file, update it and write it back.

I do something similar on one of my projects but instead of using a JS file I'll store those values in a JSON file. After making your modifications to the object you can do something like this

fs.writeFile('file.json', JSON.stringify(myObj, null, 4), function(err) {
    if (err) {
      //handle error
    }
    else {
      //handle success
    }
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM