Promise-based power tool for common filesystem tasks
Last updated 3 years ago by rich_harris .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install sander 
SYNC missed versions from official npm registry.


A Promise-based power tool for common filesystem tasks in node.js.


npm install sander

Another wrapper around fs? Really?

Yup. Working with the low-level fs API is the fastest road to callback hell, and while a lot of the existing fs wrappers add a whole load of missing features, they don't really mitigate the fundamental suckiness of working with the filesystem in a painful, imperative way, which forces you to handle errors at every step of the journey towards the centre of the node.js pyramid of doom.

Enough! Manual filing is tedious - you need a power tool. Instead of writing this...

var path = require( 'path' ),
    fs = require( 'fs' ),
    mkdirp = require( 'mkdirp' );

var dest = path.resolve( basedir, filename );

mkdirp( path.dirname( dest ), function ( err ) {
  if ( err ) throw err;

  fs.writeFile( dest, data, function ( err ) {
    if ( err ) throw err;

...write this:

var sander = require( 'sander' );
sander.writeFile( basedir, filename, data ).then( doTheNextThing );

It uses graceful-fs rather than the built-in fs module, to eliminate EMFILE from the list of things you have to worry about.



All async methods (those whose fs equivalents would take a callback, e.g. sander.readFile) return a Promise. If you're not familiar with Promises, read up on them on MDN - they're coming in ES6 and are already supported in many browsers, and I guarantee they'll make your life easier.

(Node doesn't natively support promises yet - we're using es6-promise for maximum compatibility. For convenience, the Promise constructor is exposed as sander.Promise.)

Intermediate folder creation

When writing files and folders, intermediate folders are automatically created as necessary. (I've never encountered a situation where I wanted an ENOENT error instead of having this be done for me.)

Automatic path resolution

Wherever appropriate, method arguments are joined together with path.resolve() - so the following are equivalent:

sander.readFile( 'foo', 'bar', 'baz' );
sander.readFile( path.resolve( 'foo', 'bar', 'baz' ) );
sander.readFile( 'foo/bar/baz' ); // or 'foo\bar\baz' on Windows

Methods that involve two paths

Some operations, such as renaming files, require two paths to be specified. The convention for handling this in sander is as follows:

sander.rename( basedir, oldname ).to( basedir, newname );


fs methods

In addition to the extra methods (listed below), all fs methods have sander equivalents. The synchronous methods (those ending Sync) are the same as the fs originals except that path resolution and intermediate folder creation are automatically handled (see conventions, above). All async methods return a promise.

For more information about what these methods do, consult the node documentation.

In the list below, ...paths indicates you can use one or more strings in sequence, as per the automatic path resolution convention. An fd argument refers to a file descriptor, which you'd generate with or sander.openSync(). Arguments wrapped in [] characters are optional.

sander.appendFile(...paths, data, [options])
sander.appendFileSync(...paths, data, [options])
sander.chmod(...paths, {mode: mode})
sander.chmodSync(...paths, {mode: mode})
sander.chown(...paths, uid, gid)
sander.chownSync(...paths, uid, gid)
sander.createReadStream(...paths, [options])
sander.createWriteStream(...paths, [options])
sander.fchmod(fd, {mode: mode})
sander.fchmodSync(fd, {mode: mode})
sander.fchown(fd, uid, gid)
sander.fchownSync(fd, uid, gid)
sander.ftruncate(fd, len)
sander.ftruncateSync(fd, len)
sander.futimes(fd, atime, mtime)
sander.futimesSync(fd, atime, mtime)
sander.lchmod(...paths, {mode: mode})
sander.lchmodSync(...paths, {mode: mode})
sander.lchown(...paths, uid, gid)
sander.lchownSync(...paths, uid, gid)
sander.mkdir(...paths, [{mode: mode}])
sander.mkdirSync(...paths, [{mode: mode}]), flags, [{mode: mode}])
sander.openSync(...paths, flags, [{mode: mode}]), buffer, offset, length, position)
sander.readSync(fd, buffer, offset, length, position)
sander.readFile(...paths, [options])
sander.readFileSync(...paths, [options])
sander.realpath(...paths, [cache])
sander.realpathSync(...paths, [cache])
sander.symlink(...paths).to(...paths, [{type: type}])
sander.symlinkSync(...paths).to(...paths, [{type: type}])
sander.truncate(...paths, len)
sander.truncateSync(...paths, len)
sander.utimes(...paths, atime, mtime)
sander.utimesSync(...paths, atime, mtime)
sander.unwatchFile(...paths, [listener]), [options], [listener])
sander.watchFile(...paths, [options], listener)
sander.write(fd, buffer, offset, length, position)
sander.writeSync(fd, buffer, offset, length, position)
sander.writeFile(...paths, data, [options])
sander.writeFileSync(...paths, data, [options])

Note that with the chmod/fchmod/lchmod/symlink/mkdir/open methods (and their synchronous equivalents), the mode and type arguments must be passed as objects with a mode or type property. This is so that sander knows which arguments should be treated as parts of a path (because they're strings) and which shouldn't.

The same is true for methods like readFile - whereas in node you can do fs.readFile('path/to/file.txt', 'utf-8') if you want to specify utf-8 encoding, with sander the final argument should be a {encoding: 'utf-8'} object.

Extra methods

// Copy a file using streams. `readOptions` is passed to `fs.createReadStream`,
// while `writeOptions` is passed to `fs.createWriteStream`
sander.copyFile(...paths, [readOptions]).to(...paths, [writeOptions])

// Copy a file synchronously. `readOptions`, is passed to `fs.readFileSync`,
// while `writeOptions` is passed to `fs.writeFileSync`
sander.copyFileSync(...paths, [readOptions]).to(...paths, [writeOptions])

// Copy a directory, recursively. `readOptions` and `writeOptions` are
// treated as per `sander.copyFile[Sync]`
sander.copydir(...paths, [readOptions]).to(...paths, [writeOptions])
sander.copydirSync(...paths, [readOptions]).to(...paths, [writeOptions])

// List contents of a directory, recursively

// Remove a directory and its contents

// Symlink a file or directory, unless we're on Windows in which
// case fall back to copying to avoid permissions issues



Current Tags

  • 0.6.0                                ...           latest (3 years ago)

24 Versions

  • 0.6.0                                ...           3 years ago
  • 0.5.1                                ...           4 years ago
  • 0.5.0                                ...           4 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.8                                ...           4 years ago
  • 0.3.7                                ...           4 years ago
  • 0.3.6                                ...           4 years ago
  • 0.3.5                                ...           4 years ago
  • 0.3.4                                ...           5 years ago
  • 0.3.3                                ...           5 years ago
  • 0.3.2                                ...           5 years ago
  • 0.3.1                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.4                                ...           5 years ago
  • 0.2.3                                ...           5 years ago
  • 0.2.2                                ...           5 years ago
  • 0.2.1                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.6                                ...           5 years ago
  • 0.1.5                                ...           5 years ago
  • 0.1.3                                ...           5 years ago
  • 0.1.2                                ...           5 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
Maintainers (1)
Today 0
This Week 169
This Month 583
Last Day 28
Last Week 367
Last Month 1,940
Dependencies (3)
Dev Dependencies (4)
Dependents (111)

Copyright 2014 - 2016 © |