Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Complement to Node.js fs package
Latest commit 101749b @medikoo Fix promises handling issue
Failed to load latest commit information.
lib Move out main modules from lib folder
test Fix test order
.gitignore Initial (derived from node-ext project)
.lint Add promise version of appendFile
.lintignore Initial (derived from node-ext project)
.travis.yml Update Travis CI configuration
CHANGES v0.2.3
LICENSE Rename LICENCE to LICENSE Update documentation
append-file.js Add promise version of appendFile
chmod.js Move out main modules from lib folder
copy.js Fix promises handling
descriptors-handler.js Be inteligent against initialized state
index.js Add promise version of appendFile
is-ignored.js Move out main modules from lib folder
lchmod.js Move out main modules from lib folder
lstat.js Move out main modules from lib folder
mkdir.js Move out main modules from lib folder
package.json v0.2.3
read-file.js Move out main modules from lib folder
readdir.js Fix promises handling issue
rename.js Fix intermediate handling
rmdir.js Fix error handling
stat.js Move out main modules from lib folder
symlink.js Move out main modules from lib folder
type-by-stats.js Move out main modules from lib folder
unlink.js Move out main modules from lib folder
watch-path.js Move out main modules from lib folder
watch.js Move out main modules from lib folder
write-file.js Support override of mode


Functions that complement and extend fs package

Originally derived from node-ext package.


$ npm install fs2


chmod(path, mode[, cb]) (fs2/chmod)

Same as fs.chmod. Returns promise.

Not available on Windows.

copy(src, dest[, options[, cb]]) (fs2/copy)

Copy file, returns promise but accepts as well regular callback. Eventual options are passed to underlying fs.createWriteStream

descriptorsHandler() (fs2/descriptors-handler)

Initializes EMFILE errors prevention.

To be used only in main modules. Never require it in generic module that may be required in others

How it works? If limit of descriptors is reached it holds the calls to native functions and releases them when taken descriptors are freed.

Internally it provides same solution as fs-graceful module with following differences:

  1. Focuses only on file descriptors limit problem
  2. Gives access to taken/available descriptors count and allows setting of limit by external module. Thanks to that we can also cover descriptors opened by module (watch is bound to that module)
  3. Covers readdir calls (which also happen to throw EMFILE errors)
  4. More bulletproof (I assume) error handling logic

isIgnored(mode, path[, options[, cb]]) (fs2/is-ignored)

Whether file is ignored up to predefined rules. Returns promise but regular callback is also supported.

Rules are decided by mode argument. Currently only git mode is supported, in that case rules are searched in .gitignore files (Rules have effect only if placed in valid git repositories). Other modes can be easily configured by extending _ignoreModes module (See lib/fs/_ignore-modes directory to see how it's done).

Supported options:

  • globalRules string|array - additional global rules. They will be matched as if placed in filesystem root directory, it means that any rules found in existing ignore files may override them.
  • watch bool - whether to watch for changes. If ignore state would change, returned promise would emit change event with new value (true/false)

lchmod(path, mode[, cb]) (fs2/lchmod)

Same as fs.lchmod. Returns promise.

Only available on Mac OS X.

lstat(path[, cb]) (fs2/lstat)

Same as fs.lstat. Returns promise.

mkdir(path[, options|mode[, cb]]) (fs2/mkdir)

Extended version of native mkdir. Returns promise

Supported options:

  • mode - Reflects mode in native version
  • intermediate - Whether to create directories recursively (if parent is not created), reflects mkir -p, internal implementation inspired by Substack's node-mkdirp

readFile(path[, options][, cb]) (fs2/read-file)

Extended version of native fs.readFile. Returns promise

Supported options:

  • loose - Do not error if file doesn't exits or is inaccessible, return null instead.
  • watch - Whether to watch file for changes. Changes are emited via change event on returned promise. If file was removed and loose option is off, end event is emitted and watcher is closed

readdir(path[, options[, cb]]) (fs2/readdir)

Extended version of native fs.readdir. Returns promise

Suported options:

  • depth number- Level of recurse into subdirectories. Defaults to 0 which resembles behavior of native version. If you want to recurse without any nest limitation just provide Infinity
  • type object- Which type of files should be returned. By default all files are returned. Stats methods shows how many different types can be returned. To narrow it down provide a hash. e.g. { file: true, symbolicLink: true }.
  • pattern regexp- Filter returned files by specific pattern. Pattern should be regular expression that would be matched against full path.
  • watch bool - Watch directory for changes. Changes are emitted on returned promise with data events. event object states which files were added (event.added) and which were removed (event.removed), Starting from next release (v0.4) this functionality will most likely be provided as valid Node.js stream
  • stream bool - Whether to provide data continuously. Currently it's not provided as a stream per se (it would be starting from next release, v0.4), data is emited as data events on returned promise object, structure of event objects described under watch option
  • ignoreRules string|array - Whether to obey ignore rules found in ignore files. See fs.isIgnored for more information
  • globalRules string|array - Global rules that complement ignoreRules. See fs.isIgnored for more information.

rename(oldPath, newPath[, cb]) (fs2/rename)

Same as fs.rename. Returns promise.

rmdir(path[, options[, cb]]) (fs2/rmdir)

Extended version of native rmdir. Returns promise

Supported options:

  • recursive - Attempt to remove directory with subdirectories recursively.
  • force - Attempt to remove other files within directory as well.

stat(path[, cb]) (fs2/stat)

Same as fs.stat. Returns promise.

symlink(srcPath, dstPath[, type[, cb]]) (fs2/symlink)

Same as fs.symlink. Returns promise.

typeByStats(stats) (fs2/type-by-stats)

Returns type of file according to provided stats object.

unlink(path[, cb]) (fs2/unlink)

Same as fs.unlink. Returns promise.

watchPath(path) (fs2/watch-path)

Watch specific path for changes. It's about observing specific file path (not directory content). change events are emitted with event object where event.type says wether file was created, modified or removed.

watch(path) (fs2/watch)

Watch file for changes. wrapper that works same way on every platform, always configured in persistent: false mode. It's aware of open file descriptors limitations, if EMFILE error is approach, switch to alternative mode that pings file stats (see fs.watchFile) is made.

writeFile(filename, data[, options|encoding[, callback]]) (fs2/write-file)

Same as native fs.writeFile but safe for simultaneous calls of write to same file (in such case current write will be abandonded, and new would be started).

Supported options:

  • encoding - Reflects encoding in native version
  • intermediate - In case directory doesn't exist, whether to create full directory path

Tests Build Status

$ npm test
Something went wrong with that request. Please try again.