# browserify-optional

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

Latest version **1.0.1** (published 2017-08-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install browserify-optional
pnpm add browserify-optional
yarn add browserify-optional
bun add browserify-optional
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2017-08-28 |
| First published | 2015-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Devon Govett |
| Maintainers | devongovett |
| Keywords | browserify, transform, optional |

## Links

- npm: https://www.npmjs.com/package/browserify-optional
- Repository: https://github.com/devongovett/browserify-optional
- Issues: https://github.com/devongovett/browserify-optional/issues
- npm.io page: https://npm.io/package/browserify-optional

## Dependencies (3)

- [ast-types](https://npm.io/package/ast-types.md) ^0.7.0
- [ast-transform](https://npm.io/package/ast-transform.md) 0.0.0
- [browser-resolve](https://npm.io/package/browser-resolve.md) ^1.8.1

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2017-08-28
- 1.0.0 — 2015-03-23

## README

# 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.

```javascript
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:

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

## License

MIT

---
_Source: https://npm.io/package/browserify-optional · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
