node-dir
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
minimatch | 3.1.2 | 11.66 kB | ISC | prod |
Node-dir is a lightweight Node.js module that provides methods for common directory and file operations. It parades a host of functions that support asynchronous and non-blocking methods for trasversing the directory structure. The features of node-dir include recursively getting an array of file paths, subdirectories, both files and subdirectories and methods for recursively, sequentially reading and processing the content of files in a directory and its subdirectories. This package is designed to offer flexibility options where needed and is ideal for handling asynchronous file and directory operations in a Node.js environment.
To use node-dir in your Node.js application, the first step is to install the module via npm: npm install node-dir
. Once installed, you can import node-dir into your JavaScript file: var dir = require('node-dir');
Node-dir provides various methods to handle file and directory operations. Here are a few usage examples:
To read files from a directory, you can use the readFiles
method:
var dir = require('node-dir');
dir.readFiles(__dirname,
function(err, content, next) {
if (err) throw err;
console.log('content:', content);
next();
},
function(err, files){
if (err) throw err;
console.log('finished reading files:', files);
}
);
To asynchronously iterate through files and pass an array of file paths to a callback, use the files
method:
var dir = require('node-dir');
dir.files(__dirname, function(err, files) {
if (err) throw err;
console.log(files);
});
Node-dir also supports synchronous operations using the {sync:true}
option. The files
method can be used synchronously like so:
var dir = require('node-dir');
var files = dir.files(__dirname, {sync:true});
console.log(files);
Node-dir supports promises as well:
var dir = require('node-dir');
dir.promiseFiles(__dirname)
.then((files)=>{
console.log(files);
})
.catch(e=>console.error(e))
The documentation for node-dir is found within the readme file on the official GitHub repository. The documentation provides an in-depth look at the API, including usage examples and descriptions of the various methods available. You can dive into it to better understand how each method works and how to explore the full potential of the node-dir package. The table of contents in the readme makes it easier to navigate through the documentation. Each available method such as readFiles, readFilesStream, files, promiseFiles, subdirs, paths
has been well documented.