1. read-file-relative
Read files with path relative to the current module without annoying boilerplate code
read-file-relative
Package: read-file-relative
Created by: inikulin
Last modified: Sun, 26 Jun 2022 09:13:26 GMT
Version: 1.2.0
License: MIT
Downloads: 943,333
Repository: https://github.com/inikulin/read-file-relative

Install

npm install read-file-relative
yarn add read-file-relative

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 ([email protected])

Dependencies

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code