Socket
Socket
Sign inDemoInstall

browserify-optional

Package Overview
Dependencies
11
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    browserify-optional

A browserify transform that allows optional dependencies in try..catch blocks


Version published
Weekly downloads
740K
increased by0.98%
Maintainers
1
Install size
1.46 MB
Created
Weekly downloads
 

Readme

Source

browserify-optional

It is a common pattern in Node to support optional dependencies via requires in try..catch blocks. Browserify doesn't support this by default and throws a compile time error when it cannot find a module. You can solve the problem by using browserify's exclude option, but this works globally instead of at a per-module level. This transform fixes the problem by moving the compile time error to a runtime error for requires of missing modules inside try..catch blocks.

Example

The transform would transform the following code such that requiring missing-module would throw a runtime error instead of a compile time error, making the code work as expected.

try {
  var x = require('missing-module');
} catch (e) {
  var x = require('replacement-module');
}

To set it up in browserify, add this to your package.json:

"browserify": {
  "transform": ["browserify-optional"]
}

License

MIT

Keywords

FAQs

Last updated on 28 Aug 2017

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc