I am using Node.js
.
I want to check if folder is empty or not? One option is to use fs.readdir but it loads whole bunch of
Duplicate from my answer in how to determine whether the directory is empty directory with nodejs
There is the possibility of using the opendir
method call that creates an iterator for the directory.
This will remove the need to read all the files and avoid the potential memory & time overhead
import {promises as fsp} from "fs"
const dirIter = await fsp.opendir(_folderPath);
const {value,done} = await dirIter[Symbol.asyncIterator]().next();
await dirIter.close()
The done value would tell you if the directory is empty