Giter Club home page Giter Club logo

lazy-require's Introduction

Lazy Require

Travis CI Build Status NPM version NPM downloads Dependency Status Dev Dependency Status
GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

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.

Usage

// 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

npm

  • Install: npm install --save lazy-require
  • Import: import * as pkg from ('lazy-require')
  • Require: const pkg = require('lazy-require')

jspm

<script type="module">
    import * as pkg from '//dev.jspm.io/[email protected]'
</script>

This package is published with the following editions:

  • lazy-require aliases lazy-require/source/index.js
  • lazy-require/source/index.js is ESNext source code for Node.js with Require for modules
  • lazy-require/edition-browsers/index.js is ESNext compiled for web browsers with Require for modules

History

Discover the release history by heading on over to the HISTORY.md file.

Contribute

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

Backers

Maintainers

These amazing people are maintaining this project:

Sponsors

No sponsors yet! Will you be the first?

GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

Contributors

These amazing people have contributed code to this project:

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

License

Unless stated otherwise all works are:

and licensed under:

lazy-require's People

Contributors

balupton avatar dependabot-preview[bot] avatar robloach avatar

Stargazers

Szikszai László avatar Ore Poran avatar shedali avatar Cat  avatar Kevin.Y avatar Sam Gluck avatar ig avatar David Weinstein avatar samy kamkar avatar Francois-Guillaume Ribreau avatar Naoki Shibayama avatar Shannon Moeller avatar Jesús Leganés-Combarro avatar Charlike Mike Reagent avatar Glavin Wiechert avatar Matej Simek avatar  avatar

Watchers

James Cloos avatar

lazy-require's Issues

`save` option has no effect in npm >=5

The implementation of the save option works by appending a --save flag if the option is provided, otherwise just calling npm install ...:

https://github.com/bevry/lazy-require/blob/34a804f02c011d3da6ed0685b2602085c9b49b4a/source/index.js#L249-L253

Unfortunately, this doesn't work after npm 5, which changed the default behavior of npm install to save by default:

npm will --save by default now

https://blog.npmjs.org/post/161081169345/v500

Thus, regardless if save option is passed as true or false or omitted altogether, it will always effect the save behavior.

Incompatible with node 0.10

The package is a dependecy of another package I would I use (docpad) on node 0.10.* . Currently i can't, because lazy-require depends on the Symbol datatype.

Do you have any suggestion how can I solve the issue?

Here is a simplifed example, by only requireing the package in node 0.10.45:

$ node -v
v0.10.45
11:46 ajnasz@masina:~/tmp/ff $ npm -v
2.15.1
11:46 ajnasz@masina:~/tmp/ff $ npm install lazy-require
11:47 ajnasz@masina:~/tmp/ff $ cat a.js
require('lazy-require');

And when I try to run the script:

$ node a.js

/home/ajnasz/tmp/ff/node_modules/lazy-require/source/index.js:51
        let result = lazyRequire.require(name, opts)
            ^^^^^^
Error: The package lazy-require has no suitable edition for this environment, the last edition failed with:
Error: Unable to load package lazy-require edition at /home/ajnasz/tmp/ff/node_modules/lazy-require/es2015/index.js with syntax:
es2015, javascript, require
Error: The package safeps has no suitable edition for this environment, the last edition failed with:
Error: Unable to load package safeps edition at /home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/safeps/es2015/index.js with syntax:
es2015, javascript, require
ReferenceError: Symbol is not defined
    at custom (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/safeps/node_modules/taskgroup/node_modules/extendr/es2015/index.js:26:29)
    at Object.defaults (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/safeps/node_modules/taskgroup/node_modules/extendr/es2015/index.js:110:16)
    at new TaskGroup (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/safeps/node_modules/taskgroup/es2015/lib/taskgroup.js:490:11)
    at Object.<anonymous> (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/safeps/es2015/index.js:38:29)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.requirePackage (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/editions/es2015/index.js:78:25)
    at Object.<anonymous> (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/safeps/index.js:3:38)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/ajnasz/tmp/ff/node_modules/lazy-require/es2015/index.js:10:14)
    at Module._compile (module.js:456:26)
    at Object.requirePackage (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/editions/es2015/index.js:88:8)
    at Object.<anonymous> (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/safeps/index.js:3:38)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/ajnasz/tmp/ff/node_modules/lazy-require/es2015/index.js:10:14)
    at Module._compile (module.js:456:26)
    at Object.requirePackage (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/editions/es2015/index.js:78:25)
    at Object.<anonymous> (/home/ajnasz/tmp/ff/node_modules/lazy-require/index.js:3:38)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/ajnasz/tmp/ff/a.js:1:63)
    at Module._compile (module.js:456:26)
    at Object.requirePackage (/home/ajnasz/tmp/ff/node_modules/lazy-require/node_modules/editions/es2015/index.js:88:8)
    at Object.<anonymous> (/home/ajnasz/tmp/ff/node_modules/lazy-require/index.js:3:38)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/ajnasz/tmp/ff/a.js:1:63)
    at Module._compile (module.js:456:26)```

Your .dependabot/config.yml contained invalid details

Dependabot encountered the following error when parsing your .dependabot/config.yml:

The property '#/update_configs/0/automerged_updates/0/match/update_type' value "security" did not match one of the following values: all, security:patch, semver:patch, semver:minor, in_range

Please update the config file to conform with Dependabot's specification using our docs and online validator.

Using Lazyrequire without installing it with npm

hello, I would need a hand in installing lazyrequire without using npm, as I will have to use it without asking the end user for any installation. I added all the lazyrequire javascript files and various dependencies to my project:
image
but when I try to use the package like this:

'use strict'
var lazyRequire = require('./lazyrequire')

var cp = lazyRequire("child_process");

I receive this error:

     
   throw new Error('You must pass `require` into `lazyrequire(require)`')
        ^
Error: You must pass `require` into `lazyrequire(require)`
    at lazy (C:\Users\nicol\AppData\Local\com.diskxo.yunime\lazyrequire.js:11:15)
    at Object.<anonymous> (C:\Users\nicol\AppData\Local\com.diskxo.yunime\index.js:4:10)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1063:30)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:928:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:769:14)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)←[39m
←[90m    at internal/main/run_main_module.js:17:47←[39m
Process finished with exit code 1

Anybody can help me? Thanks^^

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.