You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.6 KiB

4 years ago
  1. # invariant
  2. [![Build Status](https://travis-ci.org/zertosh/invariant.svg?branch=master)](https://travis-ci.org/zertosh/invariant)
  3. 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)).
  4. A way to provide descriptive errors in development but generic errors in production.
  5. ## Install
  6. With [npm](http://npmjs.org) do:
  7. ```sh
  8. npm install invariant
  9. ```
  10. ## `invariant(condition, message)`
  11. ```js
  12. var invariant = require('invariant');
  13. invariant(someTruthyVal, 'This will not throw');
  14. // No errors
  15. invariant(someFalseyVal, 'This will throw an error with this message');
  16. // Error: Invariant Violation: This will throw an error with this message
  17. ```
  18. **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.
  19. ### Browser
  20. 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`.
  21. ### Node
  22. 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)