# lazy-require

> Lazy require allows you to require modules lazily, meaning that when you lazy require a module that is missing, lazy require will automatically install it for you.

Latest version **4.6.0** (published 2020-06-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install lazy-require
pnpm add lazy-require
yarn add lazy-require
bun add lazy-require
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.6.0 |
| Published | 2020-06-20 |
| First published | 2013-06-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 3 |
| Unpacked size | 37 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | 2013+ Bevry Pty Ltd |
| Maintainers | balupton, bevryme |
| Keywords | browser, dependencies, deps, esnext, lazy, node, optDependencies, optionalDependencies, require |

## Links

- npm: https://www.npmjs.com/package/lazy-require
- Repository: https://github.com/bevry/lazy-require
- Issues: https://github.com/bevry/lazy-require/issues
- Funding: https://bevry.me/fund
- npm.io page: https://npm.io/package/lazy-require

## Dependencies (3)

- [errlop](https://npm.io/package/errlop.md) ^3.7.0
- [safeps](https://npm.io/package/safeps.md) ^10.5.0
- [extract-opts](https://npm.io/package/extract-opts.md) ^4.3.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 4.6.0 (latest) — 2020-06-20
- 4.6.0-next.1593742352.14f59c28dd26de3d9f809e36d42360d24b6bed37 (next) — 2020-07-03
- 4.6.0-next.1592625008.c8b34a2b6ede883d2f6f79cc3037a357c8149d8b — 2020-06-20
- 4.5.0 — 2020-06-10
- 4.5.0-next.1591798235.6f80235c06876d08ab41119f53768eb8d3016b69 — 2020-06-10
- 4.4.0 — 2020-06-10
- 4.4.0-next.1591781122.3492e5078785e2fc363e9c321a5e2f7d48bf736c — 2020-06-10
- 4.3.0 — 2020-05-21
- 4.3.0-next.1590077488.737148f72a18d7af2719a1966a28fbaf27d7b334 — 2020-05-21
- 4.2.0 — 2020-05-21
- 4.2.0-next.1590051062.749035fdfae501719a7f29d61c0af97a44e5e509 — 2020-05-21
- 4.1.0 — 2020-05-20
- 4.1.0-next.1590002218.55161bd01f4d2d87b2f1c87ede80d0462aef28c7 — 2020-05-20
- 4.0.0 — 2020-05-11
- 4.0.0-next.1589158074.b755edf0a64ce40929138266193f5da46b920bfe — 2020-05-11
- … 23 more at https://npm.io/package/lazy-require/versions

## README

<!-- TITLE/ -->

<h1>Lazy Require</h1>

<!-- /TITLE -->

<!-- BADGES/ -->

<span class="badge-travisci"><a href="http://travis-ci.com/bevry/lazy-require" title="Check this project's build status on TravisCI"><img src="https://img.shields.io/travis/com/bevry/lazy-require/master.svg" alt="Travis CI Build Status" /></a></span>
<span class="badge-npmversion"><a href="https://npmjs.org/package/lazy-require" title="View this project on NPM"><img src="https://img.shields.io/npm/v/lazy-require.svg" alt="NPM version" /></a></span>
<span class="badge-npmdownloads"><a href="https://npmjs.org/package/lazy-require" title="View this project on NPM"><img src="https://img.shields.io/npm/dm/lazy-require.svg" alt="NPM downloads" /></a></span>
<span class="badge-daviddm"><a href="https://david-dm.org/bevry/lazy-require" title="View the status of this project's dependencies on DavidDM"><img src="https://img.shields.io/david/bevry/lazy-require.svg" alt="Dependency Status" /></a></span>
<span class="badge-daviddmdev"><a href="https://david-dm.org/bevry/lazy-require#info=devDependencies" title="View the status of this project's development dependencies on DavidDM"><img src="https://img.shields.io/david/dev/bevry/lazy-require.svg" alt="Dev Dependency Status" /></a></span>
<br class="badge-separator" />
<span class="badge-githubsponsors"><a href="https://github.com/sponsors/balupton" title="Donate to this project using GitHub Sponsors"><img src="https://img.shields.io/badge/github-donate-yellow.svg" alt="GitHub Sponsors donate button" /></a></span>
<span class="badge-patreon"><a href="https://patreon.com/bevry" title="Donate to this project using Patreon"><img src="https://img.shields.io/badge/patreon-donate-yellow.svg" alt="Patreon donate button" /></a></span>
<span class="badge-flattr"><a href="https://flattr.com/profile/balupton" title="Donate to this project using Flattr"><img src="https://img.shields.io/badge/flattr-donate-yellow.svg" alt="Flattr donate button" /></a></span>
<span class="badge-liberapay"><a href="https://liberapay.com/bevry" title="Donate to this project using Liberapay"><img src="https://img.shields.io/badge/liberapay-donate-yellow.svg" alt="Liberapay donate button" /></a></span>
<span class="badge-buymeacoffee"><a href="https://buymeacoffee.com/balupton" title="Donate to this project using Buy Me A Coffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg" alt="Buy Me A Coffee donate button" /></a></span>
<span class="badge-opencollective"><a href="https://opencollective.com/bevry" title="Donate to this project using Open Collective"><img src="https://img.shields.io/badge/open%20collective-donate-yellow.svg" alt="Open Collective donate button" /></a></span>
<span class="badge-crypto"><a href="https://bevry.me/crypto" title="Donate to this project using Cryptocurrency"><img src="https://img.shields.io/badge/crypto-donate-yellow.svg" alt="crypto donate button" /></a></span>
<span class="badge-paypal"><a href="https://bevry.me/paypal" title="Donate to this project using Paypal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" alt="PayPal donate button" /></a></span>
<span class="badge-wishlist"><a href="https://bevry.me/wishlist" title="Buy an item on our wishlist for us"><img src="https://img.shields.io/badge/wishlist-donate-yellow.svg" alt="Wishlist browse button" /></a></span>

<!-- /BADGES -->

<!-- DESCRIPTION/ -->

Lazy require allows you to require modules lazily, meaning that when you lazy require a module that is missing, lazy require will automatically install it for you.

<!-- /DESCRIPTION -->

## Usage

```javascript
// Import
var lazyRequire = require('lazy-require')

// Attempt to load the module `ambi`, if it doesn't exist, then try to install it and load it again
// Do this synchronously, only available in Node 0.12 and above
// Passing a callback as the 3rd argument will do this asynchronously, available in all node versions
var ambi = lazyRequire('ambi', {
    /* options */
})
if (ambi instanceof Error) {
    // Error ....
    console.log('ambi failed to load because of:', ambi.stack)
} else {
    // Success ...
}

// Attempt to load the module `ambi`, if it doesn't exist, then try to install it and load it again
// Do this synchronously but with a callback, only available in Node 0.12 and above
lazyRequire.sync(
    'ambi',
    {
        /* options */
    },
    function (err, ambi) {
        // Error ...
        if (err)
            return console.log('ambi failed to load because of:', err.stack)

        // Success ...
    }
)

// Attempt to load the module `ambi`, if it doesn't exist, then try to install it and load it again
// Do this asynchronously, available in all node versions
lazyRequire.async(
    'ambi',
    {
        /* options */
    },
    function (err, ambi) {
        // Error ...
        if (err)
            return console.log('ambi failed to load because of:', err.stack)

        // Success ...
    }
)

// Attempt to load the module `ambi`, if it doesn't exist, then try to install it and load it again
// Do this synchronously if supported (Node 0.12 and above), otherwise do it asynchronously
lazyRequire.auto(
    'ambi',
    {
        /* options */
    },
    function (err, ambi) {
        // Error ...
        if (err)
            return console.log('ambi failed to load because of:', err.stack)

        // Success ...
    }
)
```

The following options are supported:

-   `cwd` the module path that you would like the requested package to be installed to if it doesn't exist, always recommended
-   `save` if the requested package doesn't exist, would you like to do a `npm --save` for it to add it to your `cwd`'s `package.json` dependencies?

There's some other methods also available to you, for now, read the source to discover them.

<!-- INSTALL/ -->

<h2>Install</h2>

<a href="https://npmjs.com" title="npm is a package manager for javascript"><h3>npm</h3></a>

<ul>
<li>Install: <code>npm install --save lazy-require</code></li>
<li>Import: <code>import * as pkg from ('lazy-require')</code></li>
<li>Require: <code>const pkg = require('lazy-require')</code></li>
</ul>

<a href="https://jspm.io" title="Native ES Modules CDN"><h3>jspm</h3></a>

```html
<script type="module">
    import * as pkg from '//dev.jspm.io/lazy-require@4.6.0'
</script>
```

<h3><a href="https://editions.bevry.me" title="Editions are the best way to produce and consume packages you care about.">Editions</a></h3>

<p>This package is published with the following editions:</p>

<ul><li><code>lazy-require</code> aliases <code>lazy-require/source/index.js</code></li>
<li><code>lazy-require/source/index.js</code> is <a href="https://en.wikipedia.org/wiki/ECMAScript#ES.Next" title="ECMAScript Next">ESNext</a> source code for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li>
<li><code>lazy-require/edition-browsers/index.js</code> is <a href="https://en.wikipedia.org/wiki/ECMAScript#ES.Next" title="ECMAScript Next">ESNext</a> compiled for web browsers with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li></ul>

<!-- /INSTALL -->

<!-- HISTORY/ -->

<h2>History</h2>

<a href="https://github.com/bevry/lazy-require/blob/master/HISTORY.md#files">Discover the release history by heading on over to the <code>HISTORY.md</code> file.</a>

<!-- /HISTORY -->

<!-- CONTRIBUTE/ -->

<h2>Contribute</h2>

<a href="https://github.com/bevry/lazy-require/blob/master/CONTRIBUTING.md#files">Discover how you can contribute by heading on over to the <code>CONTRIBUTING.md</code> file.</a>

<!-- /CONTRIBUTE -->

<!-- BACKERS/ -->

<h2>Backers</h2>

<h3>Maintainers</h3>

These amazing people are maintaining this project:

<ul><li><a href="https://github.com/balupton">Benjamin Lupton</a> — <a href="https://github.com/bevry/lazy-require/commits?author=balupton" title="View the GitHub contributions of Benjamin Lupton on repository bevry/lazy-require">view contributions</a></li></ul>

<h3>Sponsors</h3>

No sponsors yet! Will you be the first?

<span class="badge-githubsponsors"><a href="https://github.com/sponsors/balupton" title="Donate to this project using GitHub Sponsors"><img src="https://img.shields.io/badge/github-donate-yellow.svg" alt="GitHub Sponsors donate button" /></a></span>
<span class="badge-patreon"><a href="https://patreon.com/bevry" title="Donate to this project using Patreon"><img src="https://img.shields.io/badge/patreon-donate-yellow.svg" alt="Patreon donate button" /></a></span>
<span class="badge-flattr"><a href="https://flattr.com/profile/balupton" title="Donate to this project using Flattr"><img src="https://img.shields.io/badge/flattr-donate-yellow.svg" alt="Flattr donate button" /></a></span>
<span class="badge-liberapay"><a href="https://liberapay.com/bevry" title="Donate to this project using Liberapay"><img src="https://img.shields.io/badge/liberapay-donate-yellow.svg" alt="Liberapay donate button" /></a></span>
<span class="badge-buymeacoffee"><a href="https://buymeacoffee.com/balupton" title="Donate to this project using Buy Me A Coffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg" alt="Buy Me A Coffee donate button" /></a></span>
<span class="badge-opencollective"><a href="https://opencollective.com/bevry" title="Donate to this project using Open Collective"><img src="https://img.shields.io/badge/open%20collective-donate-yellow.svg" alt="Open Collective donate button" /></a></span>
<span class="badge-crypto"><a href="https://bevry.me/crypto" title="Donate to this project using Cryptocurrency"><img src="https://img.shields.io/badge/crypto-donate-yellow.svg" alt="crypto donate button" /></a></span>
<span class="badge-paypal"><a href="https://bevry.me/paypal" title="Donate to this project using Paypal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" alt="PayPal donate button" /></a></span>
<span class="badge-wishlist"><a href="https://bevry.me/wishlist" title="Buy an item on our wishlist for us"><img src="https://img.shields.io/badge/wishlist-donate-yellow.svg" alt="Wishlist browse button" /></a></span>

<h3>Contributors</h3>

These amazing people have contributed code to this project:

<ul><li><a href="https://github.com/balupton">Benjamin Lupton</a> — <a href="https://github.com/bevry/lazy-require/commits?author=balupton" title="View the GitHub contributions of Benjamin Lupton on repository bevry/lazy-require">view contributions</a></li>
<li><a href="https://github.com/RobLoach">Rob Loach</a> — <a href="https://github.com/bevry/lazy-require/commits?author=RobLoach" title="View the GitHub contributions of Rob Loach on repository bevry/lazy-require">view contributions</a></li></ul>

<a href="https://github.com/bevry/lazy-require/blob/master/CONTRIBUTING.md#files">Discover how you can contribute by heading on over to the <code>CONTRIBUTING.md</code> file.</a>

<!-- /BACKERS -->

<!-- LICENSE/ -->

<h2>License</h2>

Unless stated otherwise all works are:

<ul><li>Copyright &copy; 2013+ <a href="http://bevry.me">Bevry Pty Ltd</a></li></ul>

and licensed under:

<ul><li><a href="http://spdx.org/licenses/MIT.html">MIT License</a></li></ul>

<!-- /LICENSE -->

---
_Source: https://npm.io/package/lazy-require · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
