Skip to content

invariant

Size
0.16 kb
View source

Catch runtime errors early by asserting expected conditions. For enhanced security in production, you can configure it to strip error messages.

Usage

  1. Configure message stripping: To avoid exposing error messages in production, set stripMessage in your app’s entrypoint.

    If omitted invariant will display error messages.

    index.ts
    import { invariant, isServer } from 'crustack/utils'
    // in production, strip the error message on the client.
    invariant.config({
    stripMessage: !isServer && process.env.NODE_ENV === 'production',
    })
  2. 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 { invariant } from 'crustack/utils'
    invariant(condition, 'The condition must be truthy.')