What is klaw-sync?
The klaw-sync npm package is a Node.js module that allows users to recursively walk ('klaw') through the file system synchronously. It is useful for tasks such as reading all files in a directory and its subdirectories, filtering files by certain criteria, and obtaining file stats in a synchronous manner.
What are klaw-sync's main functionalities?
Recursive file listing
This feature allows you to list all files and directories within a given directory recursively. The example code lists all paths within '/some/directory'.
const klawSync = require('klaw-sync');
const paths = klawSync('/some/directory');
console.log(paths);
Filtering files
This feature allows you to filter the files and directories based on a custom function. In the example, only '.txt' files are listed.
const klawSync = require('klaw-sync');
const path = require('path');
const filterFn = item => path.extname(item.path) === '.txt';
const txtFiles = klawSync('/some/directory', { filter: filterFn });
console.log(txtFiles);
Including file stats
This feature allows you to include file stats in the output. The example code lists directories (excluding files) within '/some/directory' and includes their stats.
const klawSync = require('klaw-sync');
const pathsWithStats = klawSync('/some/directory', { nofile: true, stats: true });
console.log(pathsWithStats);
Other packages similar to klaw-sync
glob
The 'glob' package provides similar functionality for matching files using the patterns known as 'globs'. Unlike klaw-sync, which provides a list of files by walking the directory tree, glob applies pattern matching to select files. It can be used synchronously or asynchronously.
readdirp
The 'readdirp' package is another Node.js module that reads directories recursively. It streams entry information and can be a more memory-efficient way to handle large directories. It is similar to klaw-sync but is built around a streaming interface.
node-dir
The 'node-dir' package provides a range of directory and file reading utilities. It can read files recursively and synchronously like klaw-sync, but it also offers additional utilities for reading files asynchronously, reading the contents of files, and more.
node-klaw-sync

klaw-sync
is a Node.js recursive and fast file system walker, which is the synchronous counterpart of klaw. It lists all files and directories inside a directory recursively and returns an array of objects that each object has two properties: path
and stats
. path
is the full path of the file or directory and stats
is an instance of fs.Stats.
Install
npm i klaw-sync
Usage
klawSync(directory[, options])
directory
<String>
options
<Object>
(optional)
nodir
<Boolean>
default: undefined
- return only files (ignore directories).
nofile
<Boolean>
default: undefined
- return only directories (ignore files).
depthLimit
: <Number>
default: -1
- the number of times to recurse before stopping.
-1
for unlimited.
fs
: <Object>
default: graceful-fs
- custom
fs
, useful when mocking fs
object.
filter
<Function>
default: undefined
- function that gets one argument
fn({path: '', stats: {}})
and returns true to include or false to exclude the item.
traverseAll
<Boolean>
default: undefined
- traverse all subdirectories, regardless of
filter
option. This can be useful when you have a filter function and still want to traverse all subdirectories even if your filter function doesn't pass for some directories.
- Return:
<Array<Object>>
[{path: '', stats: {}}]
Examples
const klawSync = require('klaw-sync')
const paths = klawSync('/some/dir')
catch error
const klawSync = require('klaw-sync')
let paths
try {
paths = klawSync('/some/dir')
} catch (er) {
console.error(er)
}
console.dir(paths)
files only
const klawSync = require('klaw-sync')
const files = klawSync('/some/dir', {nodir: true})
directories only
const klawSync = require('klaw-sync')
const dirs = klawSync('/some/dir', {nofile: true})
ignore hidden directories
const path = require('path')
const klawSync = require('klaw-sync')
const filterFn = item => {
const basename = path.basename(item.path)
return basename === '.' || basename[0] !== '.'
}
const paths = klawSync('/some/dir', { filter: filterFn})
filter based on stats
Here traverseAll
option is required since we still want to read all subdirectories even if they don't pass the filter
function, to see if their contents do pass the filter
function.
const klawSync = require('klaw-sync')
const refTime = new Date(2017, 3, 24).getTime()
const filterFn = item => item.stats.mtime.getTime() > refTime
const paths = klawSync('/some/dir', { traverseAll: true, filter: filterFn })
Run tests
lint: npm run lint
unit test: npm run unit
lint & unit: npm test
benchmark: npm run benchmark
Performance compare to other similar modules
Running some benchmark tests on these modules:
(as of Jan 25, 2017) klaw-sync
is the fastest module!
results (tested on Ubuntu 18.04, Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz, 8 CPUs, 8g RAM, node v10.9.0)
Running benchmark tests..
root dir length: 1110
walk-sync x 80.71 ops/sec ±1.42% (72 runs sampled)
klaw-sync x 160 ops/sec ±1.17% (79 runs sampled)
Fastest is klaw-sync
root dir length: 11110
walk-sync x 7.55 ops/sec ±3.39% (23 runs sampled)
klaw-sync x 14.95 ops/sec ±0.27% (40 runs sampled)
Fastest is klaw-sync
root dir length: 111110
walk-sync x 0.63 ops/sec ±6.92% (6 runs sampled)
klaw-sync x 1.22 ops/sec ±0.96% (7 runs sampled)
Fastest is klaw-sync
Contributing
- Fork the repository
- Clone your forked version
git clone https://github.com/YOUR_USERNAME/node-klaw-sync.git
- Create a new branch for your changes
git checkout -b feature/your-feature-name
- Make your changes and commit them
- Push to your fork
git push origin feature/your-feature-name
- Create a Pull Request from your fork to the original repository
Before submitting a PR:
- Ensure tests pass:
npm test
- Add tests for new features
- Follow the existing code style
License
Licensed under MIT