JS

HOW TO WRITE FILES IN NODEJS

TO WRITE require("fs").writeFile("FILE.TXT", "TEXT", (err) => {   if (err) { console.log(err); }   console.log("Written"); });

WRITE FILE (ASYNC)

01

TO APPEND require("fs").appendFile("FILE.TXT", "TEXT", (err) => {   if (err) { console.log(err); }   console.log("Written"); });

TO WRITE try {   require("fs").writeFileSync("FILE.TXT",   "TEXT"); } catch (err) { console.log(err); }

WRITE FILE (SYNC)

02

TO APPEND try {   require("fs").appendFileSync("FILE.TXT",   "TEXT"); } catch (err) { console.log(err); }

CREATE FILE STREAM const fs = require("fs"); const stream = fs.createWriteStream("FILE.TXT");   WRITE TO STREAM for (let i of ARRAY) { stream.write(i + "\r\n") }  stream.end();

WRITE FILE (STREAM)

03