1 |
3a515b92
|
cagy
|
# invariant
|
2 |
|
|
|
3 |
|
|
[](https://travis-ci.org/zertosh/invariant)
|
4 |
|
|
|
5 |
|
|
A 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 |
|
|
|
7 |
|
|
A way to provide descriptive errors in development but generic errors in production.
|
8 |
|
|
|
9 |
|
|
## Install
|
10 |
|
|
|
11 |
|
|
With [npm](http://npmjs.org) do:
|
12 |
|
|
|
13 |
|
|
```sh
|
14 |
|
|
npm install invariant
|
15 |
|
|
```
|
16 |
|
|
|
17 |
|
|
## `invariant(condition, message)`
|
18 |
|
|
|
19 |
|
|
```js
|
20 |
|
|
var invariant = require('invariant');
|
21 |
|
|
|
22 |
|
|
invariant(someTruthyVal, 'This will not throw');
|
23 |
|
|
// No errors
|
24 |
|
|
|
25 |
|
|
invariant(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 |
|
|
|
33 |
|
|
When 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 |
|
|
|
37 |
|
|
The 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)
|