Node JS Tutorial for Beginners #9 – Reading & Writing Files (fs)


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 –


Xem thêm bài viết khác:


  1. does this method of using async work with the latest version of node js because idk it's not working for me the same!!!!

  2. New Node version solution
    const fs = require('fs')

    fs.readFile('readMe.txt','utf8', (err, data) => {

    fs.writeFile('writesMe.txt', data,(err,result) => {






  3. Hey if anyone stuck upon the error like- call back must be function, then below is the given link go check it out.

  4. Any idea for this error? TIA
    throw new ERR_INVALID_CALLBACK(cb);
    this is what my code following your example look like:
    var fs = require('fs');

    fs.readFile('readme.txt','utf8', function(err, data){
    fs.writeFile('writeme.txt', data)

    }); /*I have filenames in smallcaps*/

  5. I understand everything you coded but in JavaScript we pass the arguments in function but in callback mentioned above function (err, data) {….. }
    Where does the values for err, data comes from can you clear my doubt sir

  6. For the last segment
    fs.writeFile('writeme.txt', data, function (err, _data){});

    As writeFile is asynchronous you have to define a callback function , doesn't matter if it's empty, it will still run with no syntax error and get the job done.
    Or you can simply use writeFileSync()
    Hope this helps everyone.

  7. I showed this video to the interviewer and I'm the CEO of that company now. Thanks for sharing this amazing tutorial with us.

  8. @8:40 – If you got an error like ERR_INVALID_CALLBACK: Callback must be a function
    (possibly depreciation since 2016?)

    This is referring to fs.writeFile(yourfilename, yourdata);
    You can try to use:
    async-function{ sync-function }
    async-function{ async-function }

    Example1: async-function{ sync-function }
    fs.readFile('./readme.txt', 'utf8', function(err, data){
    fs.writeFileSync('writeme.txt', data);

    Example2: async-function{ async-function }
    fs.readFile('./readme.txt', 'utf8', function(err, data){
    fs.writeFile('writeme.txt', data, function(err2, data2){
    console.log('Use this method if you want another callback function after you read the file, then write to a new file');

    You can also throw in a "dummy" function. The error requests a callback, but you don't want it to do anything:
    fs.readFile('readme.txt','utf8',function(err,data) {
    fs.writeFile('writeme.txt', data, function());

    There are probably a couple of other options, but these just popped into my head

  9. I don't know why fs module doesn't found my .txt file at the same folder. I need to concat the __dirname with '/readme.txt' string, so it's can run properly

  10. Now if we only use fs.writeFile('file.txt',data) it won't work, we have to use callback function after data like this: fs.writeFile('file.txt,data, (err)=>{ }) so that it will be worked !!!

  11. Who knows what is wrong with this code:

    let events = require("events");

    let util = require("util");

    let Person = (name) => { = name;


    util.inherits(Person, events.EventEmitter);

    let james = new Person('james');

    let mary = new Person('mary');

    let ruy = new Person('ruy');

    let people = [james, mary, ruy];

    people.forEach(person => {

    person.on('speak', msg => {

    console.log( + ' said : ' + msg);



    james.emit('speak', 'hey dudes');

    Everything is identical as in video. Can it be because I use a newer version of Node JS?

  12. My god. Java programmer here . I wanted to table flip after I saw how easy it is to readfiles in Node js and I believe this feeling of how easy it is will come back again and again:D

  13. Good day sir. I always have an error with the 'fs.writeFile' but it works on 'fs.writeFileSync' and it also works on 'fs.readFile' . what do you think is my problem sir? thank you for answering. more power. Godbless.

    P.S: this is a great Node Js tutorial .

  14. Noted:

    The asynchronous function always needs a callback function
    Ninja code:
    fs.readFile('readMe.txt', 'utf-8', function(err, data){

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

    But, I will get the below errror:
    TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

    My code:
    fs.readFile('readMe.txt', 'utf-8', function(err, data){

    fs.writeFile('writeMe.txt', data, function(err, result){

    if(err) console.log('error', err);



    This will only works and hope can help you guys.

  15. if you encounter an error said "Callback must be a function", add a function as the last parameter of fs.writeFiles() method.

    For example:

    const fs = require('fs');
    fs.readFile('readme.txt', 'utf8', function(err, data) {
    fs.writeFile('writeme.txt', data, function(){});

    Or, if you want to keep it clear and simple, use this one:

    const fs = require('fs');
    fs.readFile('readme.txt', 'utf8', function(err, data) {
    fs.writeFile('writeme.txt', data, ()=>{});

  16. you have to add err callback function for WriteFile otherwise it is givng error.

    var fs = require('fs');

    fs.readFile('readMe.txt','utf8', function(err,data){

    console.log('read complete');

    fs.writeFile('writeMe.txt',data, function(err){

    console.log('write complete');



  17. fs.readFile('readMe.txt', 'utf8', function (err, data)

    fs.writeFile('writeMe.txt', data, function(err, result)

    if(err) console.log('error', err);



    correct code for async method of writing a file !!

  18. New version of nodeJS requires a callback function in async methods..

    var fs = require('fs');

    fs.readFile('readme.txt','utf8', function(err, data){






  19. Hi, thanks for this video and others. Just one thing (maybe more for the others): now writeFile does not work without a callback function. Node.js returns ERR_INVALID_CALLBACK error. The definition of a callback seems mandatory.


Please enter your comment!
Please enter your name here