You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

invariant

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

invariant

invariant


Version published
Weekly downloads
15M
decreased by-1.03%
Maintainers
2
Install size
30.8 kB
Created
Weekly downloads
 

Package description

What is invariant?

The invariant npm package is a utility that can be used to provide invariant checks. An invariant is a condition that can be checked to be true at certain points during the execution of a program. If the condition is false, invariant will throw an error with a message provided by the developer. This is particularly useful for catching programming errors and ensuring that certain assumptions hold true during the execution of a program.

What are invariant's main functionalities?

Basic invariant check

This feature allows developers to assert conditions within their code and throw meaningful errors if those conditions are not met. It's particularly useful for validating arguments to functions or ensuring state consistency.

const invariant = require('invariant');

function divide(a, b) {
  invariant(b !== 0, 'Cannot divide by zero.');
  return a / b;
}

divide(10, 0); // This will throw an error with the message 'Cannot divide by zero.'

Other packages similar to invariant

Readme

Source

invariant

Build Status

A mirror of Facebook's invariant (e.g. React, flux).

A way to provide descriptive errors in development but generic errors in production.

Install

With npm do:

npm install invariant

invariant(condition, message)

var invariant = require('invariant');

invariant(someTruthyVal, 'This will not throw');
// No errors

invariant(someFalseyVal, 'This will throw an error with this message');
// Error: Invariant Violation: This will throw an error with this message

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.

Browser

When used with browserify, it'll use browser.js (instead of invariant.js) and the envify transform will inline the value of process.env.NODE_ENV.

Node

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

Keywords

FAQs

Package last updated on 13 Mar 2018

Did you know?

Socket

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
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc