Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including can-write with all npm packages installed. Try it out:

var canWrite = require("can-write")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

can-write v0.1.1

Test if a file or directory is writable

can-write Build Status

Test if a file or directory is writable. Because sometimes you just gotta know.

Usage

var canWrite = require('can-write');

return canWrite('/path/to/my/file')
  .then(function(result) {
    if (result) {
      // writable
    } else {
      // not writable
    }
  }, function(err) {
    // some sort of error
  });
  
// alternatively:
  
canWrite('/path/to/my/file', function(err, result) {
  if (err) {
    // handle error 
  } else {
    if (result) {
    // writable
    } else {
      // not writable
    }
  }
});

Readability

Writability does not always imply readability. Think a "Drop Box", wherein guest users can put files in, but cannot read the contents.

To check readability, pass a truthy second parameter:

var canWrite = require('can-write');

return canWrite('/path/to/my/file', true)
  .then(function(result) {
    if (result) {
      // readable & writable
    } else {
      // either not readable or not writable.
      // if you need to know, call canWrite() again without the flag.
    }
  }, function(err) {
    // some sort of error
  });

Fair Warning

It's an anti-pattern to check writability.

You generally want to just do what you're going to do, then recover from an error.

For example, say you need to create a file:

fs.writeFile('/some/filepath', 'some data', function(err) {
  if (err.code === 'EACCESS') {
    // the file is not writable.
  }
});

In other words: don't use this module unless you really, really, really need to check whether something is writable.

Install

$ npm install can-write

License

© 2015 Christopher Hiller. Licensed MIT.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free