0

I just want to store my json data in a file in a particular directory using JS. I can not see the created file using the following code.

    var jsonse = JSON.stringify(submitted);
    var blob = new Blob([jsonse], {type: "application/json"});
    var file = new File([blob], "" + workerID + ".json")

JS Documentation Link would also suffice.

technazi
  • 888
  • 4
  • 21
  • 42

1 Answers1

1

Assuming you're not using a web browser which cannot write to your file system for, hopefully obvious (another question), security reasons.

You can redirect output from your script to a file.

node yourfile.js > output_file.json

Or you can use the fs module.

Writing files in Node.js

// note jsonse is the json blob
var fs = require('fs');
fs.writeFile("/tmp/test", jsonse, function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
}); 
Jonathan
  • 5,736
  • 2
  • 24
  • 22