SHARE

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.

—– COURSE LINKS:
+ Repo – https://github.com/iamshaunjp/node-js-playlist
+ Atom editor – https://atom.io/

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

SUBSCRIBE TO CHANNEL – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg?sub_confirmation=1

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

https://www.youtube.com/playlist?list=PL4cUxeGkcC9i9Ae2D9Ee1RvylH38dKuET

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

https://www.youtube.com/playlist?list=PL4cUxeGkcC9gQeDH6xYhmO-db2mhoTSrT

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

For more front-end development tutorials & to black-belt your coding skills, head over to – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg or http://thenetninja.co.uk

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

Twitter – @TheNetNinja – https://twitter.com/thenetninjauk

33 COMMENTS

  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);
    console.log(readme);
    });

  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

    fs.writeFileSync('second.txt',file);

    PS D:DesktopProjectsnodenode-2> node read-write
    Hi boys
    fs.js:584
    return binding.open(pathModule._makeLong(path), 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);

    with

    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

LEAVE A REPLY

Please enter your comment!
Please enter your name here