nodejs list all dir name
To list all directory names in Node.js, you can use the built-in fs.readdir method along with the fs.stat method to check if each item in the directory is a directory itself. Here's an example:
const fs = require('fs');
fs.readdir('/path/to/directory', (err, files) => {
if (err) {
console.log(err);
return;
}
// Filter out non-directories
const directories = files.filter(file => {
const stats = fs.statSync(`/path/to/directory/${file}`);
return stats.isDirectory();
});
console.log(directories);
});
This code will list all directory names within the specified directory path. If you want to list all directories within the current working directory, you can replace /path/to/directory with ..
原文地址: https://www.cveoy.top/t/topic/bIP0 著作权归作者所有。请勿转载和采集!