Socket
Socket
Sign inDemoInstall

soft-require

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

soft-require

Checks to see if npm is installed and loads it, if not it throws a catchable error.


Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Soft Require

Checks to see if npm is installed and loads it, if not it throws a catchable error.

Soft require is a simple library that allows you to require an npm library that may not be installed/available. This allows you to dynamically load npm libraries, and catch the error. This module does two things:

  1. It just wraps a require.resolve in a try catch, then returns the result of the require.
  2. Enforces good practices (read: passes linting) for dynamically loading npm modules.

Installation

From the command line, in the path of your project type in:

$ npm install --save safe-require

In Your Code

To dynamically load npm libraries (code is included in repo as example.js):

const softRequire = require('soft-require');

const libraries = ['chai', 'moasdf', 'mocha'];
const loaded = {};
const logger = console;

libraries.forEach((library) => {
  try {
    loaded[library] = softRequire(library);
  } catch (err) {
    logger.error(err.message);
  }
});

logger.info(`Loaded the following libraries successfully: ${Object.keys(loaded).join(', ')}`);

May your linting be successful and your libraries loaded dynamically.

To Test

If you clone this repo, you can test with: npm run test

Keywords

FAQs

Package last updated on 30 Dec 2017

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