Yo ninjas, in this Node JS tutorial, I’ll go through how we can use the ‘fs’ module in node to go out and read & write files to our computer.

+ Repo –
+ Atom editor –

You can find more front-end development tutorials on CSS, HTML, JavaScript, jQuery, WordPress & more on the channel homepage…


========== JavaScript for Beginners Playlist ==========

============ CSS for Beginners Playlist =============

============== The Net Ninja =====================

For more front-end development tutorials & to black-belt your coding skills, head over to – or

================== Social Links ==================

Twitter – @TheNetNinja –


  1. Sir. Can we write a file and then read it in full asynchronous way?
    I tried but I messed it up . haha.

    var fs = require("fs");
    var write_it= "something something something something ";

    fs.writeFile('file2.txt', write_it, 'utf8', function(err,data){
    var readme = fs.readFile('file2.txt','utf8', data);

  2. Great tuts!
    You should provide error handling:

    fs.readFile('readMe.txt', 'utf-8', function(err, data) {
    if (err) {console.log(err)}
    // Your code here

  3. writeFileSync is not working for me showing this bug.

    Reading works but writing is not working

    Actual code: read-write.js
    var fs = require('fs');

    //Synchronous method to READ FILE
    var file = fs.readFileSync('newfile.txt', 'utf8');

    // Writing files Synchronous method


    PS D:DesktopProjectsnodenode-2> node read-write
    Hi boys
    return, stringToFlags(flags), mode);

    Error: EPERM: operation not permitted, open 'D:DesktopProjectsnodenode-2second.txt'
    at Object.fs.openSync (fs.js:584:18)
    at Object.fs.writeFileSync (fs.js:1316:33)
    at Object.<anonymous> (D:DesktopProjectsnodenode-2read-write.js:10:4)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:423:7)

  4. Got this warning from node:
    DeprecationWarning: Calling an asynchronous function without callback is deprecated.

    I replaced

    fs.writeFile("writeMe.txt", data);


    fs.writeFile('writeMe.txt', data, (err) => {
    if (err) throw err;
    console.log('It is saved!');

    Hope it helps.

  5. How do you know that the callback function requires exactly two parameters where the first is error and the other is data?

  6. (node:57439) DeprecationWarning: Calling an asynchronous function without callback is deprecated.

    fs.readFile('readme.txt', 'utf8', function(read_err, data) {
    if(null === read_err) {
    fs.writeFile('writefile.txt', data, function(write_err) { console.log (write_err); });

  7. when ever I write files and then re run the program I get a unexpected unidentified token error in the .json file please help


Please enter your comment!
Please enter your name here