How to write to a text file by using Node.js

In this tutorial I will present you an easy way to write to a text file by using Node.js.

I have created two files: app.js and text.txt.

In the file text.txt I have writed something.

And in my app.js I have writed this code:

var fs = require("fs");

fs.writeFile('./text.txt','New text!');

Now, to replace the old text, I will open the command line and I will enter this code: node app.js

As you might see, with Node.js is very easy to write text to a text file.

Video tutorial:

Leave a Comment