Need help with import-lazy?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

sindresorhus
221 Stars 13 Forks MIT License 41 Commits 2 Opened issues

Description

Import a module lazily

Services available

!
?

Need anything else?

Contributors list

# 216
exec
test-fr...
nextjs
messeng...
25 commits
# 14,536
React N...
exec
emotico...
emoji-p...
2 commits
# 8,749
JavaScr...
GitHub
iphone
Svelte
2 commits
# 364,625
JavaScr...
TypeScr...
design-...
design-...
1 commit
# 5,136
exec
ecmascr...
test-fr...
async-f...
1 commit
# 24,687
JavaScr...
Node.js
eslint-...
eslint-...
1 commit
# 1,027
TypeScr...
cash
Electro...
argv-pa...
1 commit
# 75,696
CSS
circlec...
Express
GraphQL
1 commit
# 5,527
TypeScr...
typescr...
iterm2
exec
1 commit
# 6,551
chalk
exec
vuejs2
sketch-...
1 commit

import-lazy

Import a module lazily

Install

$ npm install import-lazy

Usage

// Pass in `require` or a custom import function
const importLazy = require('import-lazy')(require);
const _ = importLazy('lodash');

// Instead of referring to its exported properties directly… _.isNumber(2);

// …it's cached on consecutive calls _.isNumber('unicorn');

// Works out of the box for functions and regular properties const stuff = importLazy('./math-lib'); console.log(stuff.sum(1, 2)); // => 3 console.log(stuff.PHI); // => 1.618033

Note

Destructuring will cause it to fetch eagerly

While you may be tempted to do leverage destructuring, like this:

const {isNumber, isString} = importLazy('lodash');

Note that this will cause immediate property access, negating the lazy loading, and is equivalent to:

import {isNumber, isString} from 'lodash';

Usage with bundlers

If you're using a bundler, like Webpack, you'll have to import your modules like this in order to have them properly bundled:

const importLazy = require('import-lazy');

const _ = importLazy(() => require('lodash'))();

Related

  • resolve-from - Resolve the path of a module from a given path
  • import-from - Import a module from a given path
  • resolve-pkg - Resolve the path of a package regardless of it having an entry point
  • lazy-value - Create a lazily evaluated value
  • define-lazy-prop - Define a lazily evaluated property on an object

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.