read-file-relative
Read files with path relative to the current module without annoying boilerplate code
Last updated 5 years ago by inikulin .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install read-file-relative 
SYNC missed versions from official npm registry.

read-file-relative

Build Status

Read files with path relative to the current module without annoying boilerplate code

Well, I've expected @sindresorhus has a module for this, but he didn't.

What's going on?

If you have code like this:

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

var data = fs.readFileSync(path.join(__dirname, '/my-awesome-file')).toString();

Now you can replace it with:

var readSync = require('read-file-relative').readSync;

var data = readSync('/my-awesome-file');

That's it.

You want a plain buffer instead of string? No problem - just use optional second argument:

var readSync = require('read-file-relative').readSync;

var buffer = readSync('/my-awesome-file', true);

You like it the async way (didn't you :wink:)? Do it this way:

var read = require('read-file-relative').read;

read('/my-awesome-file', function(err, content) {
   ...
});

You can pass options or encoding like for regular fs.readFile:

var read = require('read-file-relative').read;

read('/my-awesome-file', 'utf8', function(err, content) {
   ...
});

BTW, you can just convert given path to absolute:

var toAbsPath = require('read-file-relative').toAbsPath;

var absPath = toAbsPath('/my-awesome-file');

Install

npm install read-file-relative

Author

Ivan Nikulin (ifaaan@gmail.com)

Current Tags

  • 1.2.0                                ...           latest (5 years ago)

4 Versions

  • 1.2.0                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago

Copyright 2014 - 2016 © taobao.org |