SHARE

Hey ninjas, in this Node JS tutorial I’ll show you how we can create and delete directories, as well as deleting files too.

—– 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

30 COMMENTS

  1. 1:01
    // Catch file not found exception

    var fs = require('fs');

    var fileContents;
    try {
    fileContents = fs.readFileSync('writeMe.txt', 'utf8');
    console.log(fileContents)
    } catch (err) {
    console.log('File not found :n' + err);
    }

  2. Tell me why in this deleting file case it shows Deprecate warning that "Calling an asynchronous function without callback is deprecated"..

  3. Dear Shaun,

    Thanks for these fantastic vids, you have a fantastic channel here. Could you please shortly explain which considerations you would make when choosing between these sync and non-sync methods?

    Thanks!

  4. So if I wanted to create the directory and then 3 seconds later remove it what can I do about the error that shows up in the command prompt after the function continues to run rmdir('stuff')?

    var fs = require('fs');

    fs.mkdir('newdir', function(){
    fs.readFile('readMe.txt', 'utf8', function(err, data){
    fs.writeFile('./newdir/writeMe.txt', data);
    });
    });

    setInterval(function(){
    fs.unlink('./newdir/writeMe.txt', function(){
    fs.rmdirSync('newdir');
    });
    }, 3000);

    Any thoughts?

  5. what if there are multiple files in a directory and you want to remove the folder anyway and all its contents. so is there any way to do it forcefully?

  6. ** Node come up with: 'DeprecationWarning: Calling an asynchronous function without callback is deprecated.'

    The reason being that whilst the fs.readfile has a callback function, the fs.writefile does not. So if you run fs.writefileSync this still works perfectly. **

    (Could be wrong) – Thanks so much Shuan. Great series as usual :):)

  7. Hi, these are really good tutorial, I have a question, when I run the mkdir and the readFile methods I get this message in the console.

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

    even though I have a cb in both async methods, does writeFile should also get a cb function?

  8. hi, thank you for this awsome tutorial .

    is there a was to check if a directory is not empty inside the code??
    something like

    if(dir is not empty){
    do this;
    }else{
    fs.rmdir('dirname')
    }

  9. I had to remove replace ".stuff" with "stuff" in the directory path. Didn't work with the dot. Also, I don't quite understand why we do this asynchronously with the function inside unlink. We can just have

    fs.unlink('stuff/writeMe.txt');
    fs.rmdir('stuff');

  10. Hi, Shaun. It's so pleasurable to watch and learn NodeJS through your tutorials. It's clear and informative. I do have two questions for this tutorial:

    1.) What to do (for deleting a directory) when there's a lot of files in the directory, and file name may be unknown?
    2.) How to move file(s) from one folder (directory) to another folder (directory)?

    Thank you. 🙂

LEAVE A REPLY

Please enter your comment!
Please enter your name here