https://github.com/rowanmanning/require-first
Require and return the first available module.
https://github.com/rowanmanning/require-first
library
Last synced: 4 months ago
JSON representation
Require and return the first available module.
- Host: GitHub
- URL: https://github.com/rowanmanning/require-first
- Owner: rowanmanning
- License: mit
- Created: 2023-07-25T07:06:47.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2026-02-20T08:29:24.000Z (5 months ago)
- Last Synced: 2026-02-20T12:38:44.387Z (5 months ago)
- Topics: library
- Language: JavaScript
- Homepage:
- Size: 609 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: docs/contributing.md
- License: LICENSE
- Code of conduct: docs/code_of_conduct.md
- Codeowners: .github/CODEOWNERS
- Security: docs/security.md
Awesome Lists containing this project
README
# @rowanmanning/require-first
Require and return the first available module.
* [Requirements](#requirements)
* [Usage](#usage)
* [Migration](#migration)
* [Contributing](#contributing)
* [License](#license)
## Requirements
This library requires the following to run:
* [Node.js](https://nodejs.org/) 20+
## Usage
Install with [npm](https://www.npmjs.com/):
```sh
npm install @rowanmanning/require-first
```
Load the library into your code with a `require` call:
```js
const { requireFirst } = require('@rowanmanning/require-first');
```
Require modules by passing in an array of paths. The first one that successfully resolves will be returned.
```js
const thing = requireFirst(['thing1', './thing2', '~/../example/thing3']);
```
By default, an error will be thrown if none of the modules are found. You can change this behaviour to return a default value specified by you:
```js
const thing = requireFirst(['thing1', './thing2', '~/../example/thing3'], {
example: true
});
```
## Migration
A new major version of this project is released if breaking changes are introduced. We maintain a [migration guide](docs/migration.md) to help users migrate between these versions.
## Contributing
[The contributing guide is available here](docs/contributing.md). All contributors must follow [this library's code of conduct](docs/code_of_conduct.md).
## License
Licensed under the [MIT](LICENSE) license.
Copyright © 2019, Rowan Manning