babel-plugin-unassert

raw JSON →
3.2.0 verified Sat Apr 25 auth: no javascript

Babel plugin that removes assertion calls (assert, console.assert, power-assert) from production builds. Current stable version is 3.2.0, compatible with Babel 7. Release cadence is low, with major versions tied to Babel major versions. Key differentiator: allows full use of assertions during development without runtime overhead in production, with support for ES modules and power-assert integration. Alternative to manual conditional wrapping or dead-code elimination.

error Error: Cannot find module 'babel-plugin-unassert'
cause Package not installed or incorrectly required.
fix
Run 'npm install --save-dev babel-plugin-unassert' and ensure it's in node_modules.
error Error: [BABEL] unknown: Plugin babel-plugin-unassert is not a valid plugin (Error: Cannot find module 'babel-plugin-unassert')
cause String name in Babel config but plugin not installed.
fix
Install the plugin and ensure node_modules path is correct. Check for typos.
error TypeError: babelPluginUnassert is not a function
cause Trying to use the default export incorrectly, e.g., destructuring.
fix
Use const plugin = require('babel-plugin-unassert'); not const { unassert } = require('babel-plugin-unassert');
error Assertions not removed in production build
cause Plugin not added to Babel plugins list, or NODE_ENV not set correctly.
fix
Add 'babel-plugin-unassert' to plugins array when NODE_ENV === 'production'. Verify Babel config applies during build.
breaking Babel 7 is incompatible with Babel 6; babel-plugin-unassert v3 requires Babel 7.
fix Use babel-plugin-unassert@2 for Babel 6, @1 for Babel <=5.
breaking Babel 6 is incompatible with Babel 5; babel-plugin-unassert v2 requires Babel 6.
fix Use babel-plugin-unassert@1 for Babel 5 or lower.
deprecated babel-plugin-unassert v1 is deprecated; use v2 or v3 with appropriate Babel version.
fix Upgrade to v2 (Babel 6) or v3 (Babel 7) as per your Babel version.
gotcha Plugin removes only calls to assert, console.assert, and power-assert modules. Custom assert functions are not removed.
fix Ensure your assertions use standard assert module or power-assert. Alternatively, ensure custom functions have no side effects.
gotcha Using require('babel-plugin-unassert') in Node returns the plugin function, not an object with a default property. Importing with ES named import fails.
fix Use require('babel-plugin-unassert') or import default. Never destructure as { unassert }.
npm install babel-plugin-unassert
yarn add babel-plugin-unassert
pnpm add babel-plugin-unassert

Demonstrates using babel-plugin-unassert via Babel config to remove assertions in production. Shows conditional plugin addition based on NODE_ENV.

// babel.config.js
module.exports = function (api) {
  const presets = ['@babel/env'];
  const plugins = [];

  if (process.env.NODE_ENV === 'production') {
    plugins.push('babel-plugin-unassert');
  }

  return {
    presets,
    plugins
  };
};

// src/math.js
const assert = require('assert');

function add(a, b) {
  console.assert(typeof a === 'number');
  assert(!isNaN(a));
  return a + b;
}

// After build:
// function add(a, b) {
//   return a + b;
// }