Nur Rony
Polyglot Programmer. Blindly love JS. An automation freak who love to cook code in React, Redux, RxJS and serve them using Docker. Married to the most beautiful girl.

Replacing text in a file

Replacing text in a file

Here is the simple chunk of code to replace a particular section in a file asynchronously.

fs.readFile(filePath, 'utf8', function(err, data) {  
    if (err) {
      return console.log(err);
    }

    var result = data.replace(your-regex-or-text,replacement-text);
    fs.writeFile(filePath, result, 'utf8', function(err) {
        if (err) {
           return console.log(err);
        };
    });
});