req-from

Require a module like `require()` but from a given path

Renamed to `import-from`.
Last updated 7 years ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install req-from 
SYNC missed versions from official npm registry.

req-from Build Status

Require a module like require() but from a given path

Install

$ npm install --save req-from

Usage

const reqFrom = require('req-from');

// There is a file at `./foo/bar.js`

reqFrom('foo', './bar');

API

reqFrom(fromDir, moduleId)

Like require(), throws when the module can't be found.

reqFrom.silent(fromDir, moduleId)

Returns null instead of throwing when the module can't be found.

fromDir

Type: string

Directory to require from.

moduleId

Type: string

What you would use in require().

Tip

Create a partial using a bound function if you want to require from the same fromDir multiple times:

const reqFromFoo = reqFrom.bind(null, 'foo');

reqFromFoo('./bar');
reqFromFoo('./baz');

Related

  • req-cwd - Require a module from the current working directory
  • resolve-from - Resolve the path of a module from a given path
  • resolve-cwd - Resolve the path of a module from the current working directory
  • resolve-pkg - Resolve the path of a package regardless of it having an entry point
  • lazy-req - Require modules lazily

License

MIT © Sindre Sorhus

Current Tags

  • 3.0.0                                ...           latest (7 years ago)

4 Versions

  • 3.0.0 [deprecated]           ...           7 years ago
  • 2.0.0                                ...           9 years ago
  • 1.0.1                                ...           10 years ago
  • 1.0.0                                ...           10 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (2)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |