required-path

Prepare path for require

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
required-path
601.0.16 years ago6 years agoMinified + gzip package size for required-path in KB

Readme

required-path
Handy helper to prepare your string for require().
!NPM Statusnpm-imgnpm !Travis Statustest-imgtravis !Windows Statusappveyor-imgappveyor !Coverage Statuscoverage-imgcoveralls !Dependency Statusdependency-imgdavid
The emergence of this module is due to the fact that some methods of path module could return “un-required” path.
For example:
path.relative('foo/bar', 'foo/baz.js') will return '../baz.js', and we could require it.
But path.relative('foo', 'foo/bar/baz.js') will return 'bar/baz.js', and such path would cause error in require.

Usage

var requiredPath = require('required-path');
var file = 'file.js';

try {
  require(file);
} catch(err) {
  err; → // "Error: Cannot find module 'file.js'"
}

var prepared = requiredPath(file);
require(prepared); → // now it's okay

License

Code released under the MIT License.