npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

try-require

v1.2.1

Published

Conditional load modules.

Downloads

355,013

Readme

try-require

try/require mechanism to conditionally load a module using require.

Installation

npm install try-require --save

Usage

try-require lets you try to require a module and not fail if the module is not installed. You could do this inline but the try/catch block will prevent V8 from optimizing your entire function. Therefore, making try-require standalone means only this module is not optimizable.

Sometimes you don't need to load the module, just determine if it is available. For this, a resolve function is provided with `try-require.``

// Conditionally require a module
var tryRequire = require('try-require');
var maybe = tryRequire('maybeModule');

// If `maybeModule` is not available, then `maybe` will
// be undefined. If available it is equivalent to:
// var maybe = require('maybeModule');
// Determine if a module is available without loading it into memory
var tryRequire = require('try-require');
var maybePath = tryRequire.resolve('maybeModule');

// If available, maybePath holds the path to the module
// and the module is not loaded. If `maybeModule` is not available,
// then `maybePath` will be undefined.

Optionally, check require and resolution exceptions with lastError. Note that lastError will return null if no error has ever been triggered, or if the most recent call to require or resolve was successful.

var tryRequire = require('try-require');
var maybe = tryRequire('notAModule');

console.error( tryRequire.lastError() );

Note that both tryRequire and tryRequire.resolve accept an optional second argument if you want to provide your own version of require.

Contribute

If you would like to add to this library, please ensure that all existing test cases pass and that all new code has proper test coverage in test/all.test.js.

To run tests, simply execute:

npm test

Also, match styles within the project where language of the file allows. Some core styles to follow are:

  • indent lines with 4 spaces
  • spaces around parameters in function definitions and calls
  • opening/closing brackets on same line

License

MIT