Crustack
@crustack/utils

invariant

Catch runtime errors early by asserting expected conditions.

Usage

Configure message stripping

In your app Entrypoint, configure message stripping to avoid exposing error messages in production.

If omitted invariant will display error messages.

import { invariant, isServer } from '@crustack/utils'

// in production, strip the error message on the client.
invariant.config({
  stripMessage: !isServer && process.env.NODE_ENV === 'production',
})

For Next.js or Remix applications, avoid configuring message stripping, as these frameworks already handle it in production.

Assert conditions

Use invariant to check conditions. Pass a condition and a descriptive error message. The message can be a string or a function to defer message creation until needed.

import {  } from '@crustack/utils'

(, 'The condition must be truthy.')

On this page