How do I change any file with any format's data or its text to same as the variable in js | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I change any file with any format's data or its text to same as the variable in js

How do I change any file with any format's data or its text to same as the variable in js For example text.txt = random text1 Variable = random text2 Than Text.txt = random text2

15th Oct 2022, 6:04 AM
heaker Gamer
1 Answer
+ 2
const fs = require("fs"); const filePath = "/some/path/file.txt"; const newContent = "Hello world!"; fs.readFile(filePath, "utf8", function (err, data) { if (err) { throw err; } console.log(data); fs.writeFile(filePath, newContent, function(err) { if (err) { throw err; } }); });
16th Oct 2022, 8:39 AM
Matthew James
Matthew James - avatar