Is node.js rmdir recursive ? Will it work on non empty directories?

后端 未结 22 1354
清歌不尽
清歌不尽 2021-01-31 13:41

The documentation for fs.rmdir is very short and doesn\'t explain the behavior of rmdir when the directory is not empty.

Q: What happens if I try to use

相关标签:
22条回答
  • 2021-01-31 13:47

    Removing NON-EMPTY directories SYNCHRONOUSLY:-

    Following is the file structure -

    var fs = require('fs');
    
    fs.unlink('./stuff/writeMe.txt',function(){
      fs.rmdirSync('stuff');
    })
    

    I am firstly removing the writeMe.txt file from stuff folder using code fs.unlink('./stuff/writeMe.txt') which makes the stuff folder empty and finally removing it using code fs.rmdirSync('stuff')

    0 讨论(0)
  • 2021-01-31 13:48

    Just a small dot among this bunch of answers, but I think it's good to point it out.

    Personally (and generally) I would prefer to use an already existing library, if there's one available, for doing the task. Taking an already existing thing means, for me and especially in the open source world, using and improving an already existing thing, which could end up in a better result than doing it on my own (I'm improving something that some one other has done).

    In this case, with a small search I found out the module fs-extra, which aims to be a replacement also for rimraf and answers to the need to remove recursively directories (apparently with async and sync versions). Furthermore, it has got a good number of stars on github and seems currently mantained: these two conditions, in addition to the fact that answers to the need, make it the way to go (almosto for a bit) for me.

    0 讨论(0)
  • 2021-01-31 13:49

    Recursive remove directory for Node.js

    It turned out that Node.js fs module does not have a method to remove the directory and its content recursively. Instead you should go through the directory structure and remove atomic items, i.e. individual files and empty directories. So I found a nice gist by Takuo Kihira at https://gist.github.com/2367067 made in JavaScript and decided to make a CoffeeScript version of it:

    0 讨论(0)
  • 2021-01-31 13:51

    Figured this was a good excuse to take a dive into the source ;)

    From what I can tell, fs.rmdir is bound to the rmdir function from unistd.h. From the POSIX man page for rmdir:

    The rmdir() function shall remove a directory whose name is given by path. The directory shall be removed only if it is an empty directory.

    If the directory is not an empty directory, rmdir() shall fail and set errno to [EEXIST] or [ENOTEMPTY].

    0 讨论(0)
  • 2021-01-31 13:51

    In addition to the correct "no" answers, the rimraf package provides recursive delete functionality. It mimics rm -rf. It's also officially packaged by Ubuntu.

    0 讨论(0)
  • 2021-01-31 13:51

    Following on @geedew's answer.

    Here is an asynchronous implementation of rm -r (ie you can pass a path to a file or directory). I'm not an experienced nodejs developer and appreciate any suggestions or constructive criticism.

    var fs = require('fs');
    
    function ResultsCollector (numResultsExpected, runWhenDone) {
        this.numResultsExpected = numResultsExpected,
        this.runWhenDone = runWhenDone;
        this.numResults = 0;
        this.errors = [];
    
        this.report = function (err) {
            if (err) this.errors.push(err);
            this.numResults++;
            if (this.numResults == this.numResultsExpected) {
                if (this.errors.length > 0) return runWhenDone(this.errors);
                else return runWhenDone();
            }
        };
    }
    
    function rmRasync(path, cb) {
        fs.lstat(path, function(err, stats) {
            if (err && err.code == 'ENOENT') return cb(); // doesn't exist, nothing to do
            else if (err) {
                return cb(err);
            }
            if (stats.isDirectory()) {
                fs.readdir(path, function (err, files) {
                    if (err) return cb(err);
                    var resultsCollector = new ResultsCollector(files.length, function (err) {
                        if (err) return cb(err);
                        fs.rmdir(path, function (err) {
                            if (err) return cb(err);
                            return cb();
                        });
                    });
                    files.forEach(function (file) {
                        var filePath = path + '/' + file;
                        return rmRasync(filePath, function (err) {
                            return resultsCollector.report(err);
                        });
                    });
                });
            }
            else { // file.
                // delete file or link
                fs.unlink(path, function (err) {
                    if (err) return cb(err);
                    return cb();
                });
            }
        });
    };
    

    Invoke like so:

    rmRasync('/path/to/some/file/or/dir', function (err) {
        if (err) return console.error('Could not rm', err);
        // else success
    });
    
    0 讨论(0)
提交回复
热议问题