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.

1 Answer 1

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!");
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting Uncaught TypeError: fs.existsSync is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.