UNPKG

1.61 kBMarkdownView Raw
1# invariant
2
3[![Build Status](https://travis-ci.org/zertosh/invariant.svg?branch=master)](https://travis-ci.org/zertosh/invariant)
4
5A mirror of Facebook's `invariant` (e.g. [React](https://github.com/facebook/react/blob/v0.13.3/src/vendor/core/invariant.js), [flux](https://github.com/facebook/flux/blob/2.0.2/src/invariant.js)).
6
7A way to provide descriptive errors in development but generic errors in production.
8
9## Install
10
11With [npm](http://npmjs.org) do:
12
13```sh
14npm install invariant
15```
16
17## `invariant(condition, message)`
18
19```js
20var invariant = require('invariant');
21
22invariant(someTruthyVal, 'This will not throw');
23// No errors
24
25invariant(someFalseyVal, 'This will throw an error with this message');
26// Error: Invariant Violation: This will throw an error with this message
27```
28
29**Note:** When `process.env.NODE_ENV` is not `production`, the message is required. If omitted, `invariant` will throw regardless of the truthiness of the condition. When `process.env.NODE_ENV` is `production`, the message is optional – so they can be minified away.
30
31### Browser
32
33When used with [browserify](https://github.com/substack/node-browserify), it'll use `browser.js` (instead of `invariant.js`) and the [envify](https://github.com/hughsk/envify) transform will inline the value of `process.env.NODE_ENV`.
34
35### Node
36
37The node version is optimized around the performance implications of accessing `process.env`. The value of `process.env.NODE_ENV` is cached, and repeatedly used instead of reading `process.env`. See [Server rendering is slower with npm react #812](https://github.com/facebook/react/issues/812)