How to create a file if it does not exist?

0
Hello, I'm doing an application that will have data in JSON and would like to know, how to create a file if it does not exist? I'm using FS.

Thanks in advance.

    
asked by anonymous 20.01.2018 / 22:00

1 answer

0

On the node, you can do the following:

const fs = require("fs");
const file = "/home/user/Documents/foo.json";

if (!fs.existsSync(file)) {
    fs.writeFileSync(file, JSON.stringify({hello: "World"));
}

The script checks and the file exists and otherwise creates with an illustrative object.

    
20.01.2018 / 22:16