Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
The node-dir package is a utility library for Node.js that provides methods to read directories recursively, read files within directories, and perform other directory and file system related operations. It is useful for applications that need to handle file system operations extensively.
Reading all files in a directory recursively
This feature allows you to list all files within a directory and its subdirectories. The function `dir.files` takes a directory path and a callback function that receives an array of file paths.
const dir = require('node-dir');
dir.files('/path/to/dir', function(err, files) {
if (err) throw err;
console.log(files);
});
Reading all subdirectories within a directory
This function lists all subdirectories within a specified directory. It is useful for applications that need to analyze or manipulate directory structures.
const dir = require('node-dir');
dir.subdirs('/path/to/dir', function(err, subDirs) {
if (err) throw err;
console.log(subDirs);
});
Reading content of files in a directory
This method reads the content of each file in a directory sequentially. It provides the file content to a callback function and proceeds to the next file when the `next` function is called.
const dir = require('node-dir');
dir.readFiles('/path/to/dir', function(err, content, next) {
if (err) throw err;
console.log(content);
next();
});
fs-extra is a package that builds on the native 'fs' module, providing additional methods and simplifying file system operations. It includes methods similar to node-dir but also offers file copying, moving, and more comprehensive error handling. It is generally more robust than node-dir.
glob is a package that allows pattern matching for filenames. It can be used to find files in a directory that match a given pattern, which is somewhat similar to listing files with node-dir but with more flexibility in file selection based on patterns.
recursive-readdir is a minimalistic package specifically focused on recursively reading directories. It is similar to the recursive file listing feature of node-dir but is lighter and has fewer features, focusing only on reading directories.
A lightweight Node.js module with methods for some common directory and file operations, including asynchronous, non-blocking methods for recursively getting an array of files, subdirectories, or both, and methods for recursively, sequentially reading and processing the contents of files in a directory and its subdirectories, with several options available for added flexibility if needed.
npm install node-dir
For the sake of brevity, assume that the following line of code precedes all of the examples.
var dir = require('node-dir');
Sequentially read the content of each file in a directory, passing the contents to a callback, optionally calling a finished callback when complete. The options and finishedCallback arguments are not required.
Valid options are:
A reverse sort can also be achieved by setting the sort option to 'reverse', 'desc', or 'descending' string value.
// display contents of files in this script's directory
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);
});
// display contents of huge files in this script's directory
dir.readFilesStream(__dirname,
function(err, stream, next) {
if (err) throw err;
var content = '';
stream.on('data',function(buffer) {
content += buffer.toString();
});
stream.on('end',function() {
console.log('content:', content);
next();
});
},
function(err, files){
if (err) throw err;
console.log('finished reading files:', files);
});
// match only filenames with a .txt extension and that don't start with a `.´
dir.readFiles(__dirname, {
match: /.txt$/,
exclude: /^\./
}, 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);
});
// exclude an array of subdirectory names
dir.readFiles(__dirname, {
exclude: ['node_modules', 'test']
}, 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);
});
// the callback for each file can optionally have a filename argument as its 3rd parameter
// and the finishedCallback argument is optional, e.g.
dir.readFiles(__dirname, function(err, content, filename, next) {
console.log('processing content of file', filename);
next();
});
Asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.
dir.files(__dirname, function(err, files) {
if (err) throw err;
console.log(files);
});
Synchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.
var files = dir.files(__dirname, {sync:true});
console.log(files);
Asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.
dir.promiseFiles(__dirname)
.then((files)=>{
console.log(files);
})
.catch(e=>console.error(e))
Note that for the files and subdirs the object returned is an array, and thus all of the standard array methods are available for use in your callback for operations like filters or sorting. Some quick examples:
dir.files(__dirname, function(err, files) {
if (err) throw err;
// sort ascending
files.sort();
// sort descending
files.reverse();
// include only certain filenames
files = files.filter(function (file) {
return ['allowed', 'file', 'names'].indexOf(file) > -1;
});
// exclude some filenames
files = files.filter(function (file) {
return ['exclude', 'these', 'files'].indexOf(file) === -1;
});
});
Also note that if you need to work with the contents of the files asynchronously, please use the readFiles method. The files and subdirs methods are for getting a list of the files or subdirs in a directory as an array.
Asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of directory paths to a callback.
dir.subdirs(__dirname, function(err, subdirs) {
if (err) throw err;
console.log(subdirs);
});
Asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of both file and directory paths to a callback.
Separated into two distinct arrays (paths.files and paths.dirs)
dir.paths(__dirname, function(err, paths) {
if (err) throw err;
console.log('files:\n',paths.files);
console.log('subdirs:\n', paths.dirs);
});
Combined in a single array (convenience method for concatenation of the above)
dir.paths(__dirname, true, function(err, paths) {
if (err) throw err;
console.log('paths:\n',paths);
});
MIT licensed (See LICENSE.txt)
FAQs
asynchronous file and directory operations for Node.js
The npm package node-dir receives a total of 6,946,923 weekly downloads. As such, node-dir popularity was classified as popular.
We found that node-dir demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.