Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

require-or

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

require-or

require, or...

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

require or ...

Converts:

var someModule;
try {
    someModule = require('module-that-might-fail');
} catch (e) {
    console.log('AARGH! Could not find someModule!');
}

... into:

var requireOr = require('require-or');
var someModule = requireOr('module-that-might-fail', function () {
    console.log('AARGH! Could not find someModule!');
});

The callback will be executed synchroniously and a return value will be returned from requireOr as well, allowing:

var someModule = requireOr('some-module', function () {
    return require('some-fallback-module');
});

The callback is not mandatory, so if you just want to squelch the error message, you don't need to provide it:

var maybeSomeModule = requireOr('some-module');

Relative paths works as well:

var maybeSomeModule = requireOr('../some-module', function () {
    // ...
});

Why?

There is multiple other modules trying to solve the same problem.

  • optional
  • try-require

Both solve the problem of squelching errors, but it does not give you any improvements over vanilla require for the cases where you want to handle the fall back case.

License

This module is made public under the ISC License.

See the LICENSE file for additional details.

Keywords

FAQs

Package last updated on 01 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc