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

后端 未结 22 1416
清歌不尽
清歌不尽 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条回答
  •  猫巷女王i
    2021-01-31 14:07

    Surprisingly verbose and bad answers here...

    To delete a non-empty directory on most systems:

    import * as cp from 'child_process';
    
    const dir = '/the/dir/to/remove';
    
    const k = cp.spawn('bash');
    
    k.stdin.end(`rm -rf "${dir}"`);
    
    k.once('exit', code => {
       // check the exit code
       // now you are done
    });
    

    this will work on MacOS and Linux, but it might not work on some Windows OS.

提交回复
热议问题