als-readdir
is a Node.js module that enhances the functionality of fs.readdir
and fs.readdirSync
by adding the path
property to the Dirent
objects returned by these functions. This is particularly useful in Node.js versions prior to v20.10.0, where Dirent
objects do not include the path
property.
Additionally, als-readdir
supports custom recursive directory traversal and returns separated lists of files, directories, and any encountered errors.
- Adds the
path
property toDirent
objects returned byfs.readdir
andfs.readdirSync
. - Seamlessly integrates with both the synchronous and asynchronous versions of
readdir
. - Ensures compatibility with older versions of Node.js that do not include the
path
inDirent
. - Supports custom recursive
- returns separated files,directories and errors
To install the package, use the following command:
npm install als-readdir
const { readdirSync } = require('als-readdir');
const dirPath = '/path/to/directory';
const {files,dirs,errors} = readdirSync(dirPath, { withFileTypes: true });
files.forEach(file => {
console.log(file.name, file.path); // `file` now includes a `path` property
});
const { readdir } = require('als-readdir');
const dirPath = '/path/to/directory';
readdir(dirPath, { withFileTypes: true, recursive: true })
.then(({files,dirs,errors}) => {
files.forEach(file => {
console.log(file.name, file.path); // `file` now includes a `path` property
});
});