als-readdir
v2.2.0
Published
Enhances Node.js fs.readdir and fs.readdirSync by adding a 'path' property to Dirent objects in versions where it's not included by default.
Downloads
78
Maintainers
Readme
als-readdir
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.
Features
- 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
Installation
To install the package, use the following command:
npm install als-readdir
Usage
Synchronous Usage
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
});
Asynchronous Usage
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
});
});