babel-plugin-undebug
raw JSON → 3.0.0 verified Sat Apr 25 auth: no javascript
A Babel plugin that strips `debug` function calls from production code to reduce bundle size and eliminate debug overhead. Version 3.0.0 is ESM-only, requires Node.js 18+, and ships TypeScript types. It handles both CommonJS (`require('debug')`) and ESM (`import debug from 'debug'`) imports, removing all debug calls including side-effectful expressions like `d(value++)`. Unlike manual removal, it automates stripping without source code changes.
Common errors
error Error: Cannot find module 'babel-plugin-undebug' ↓
cause Package not installed or named incorrectly in Babel config
fix
Run
npm install --save-dev babel-plugin-undebug and ensure plugins array uses full name: 'babel-plugin-undebug' error Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/babel-plugin-undebug/index.js from ... not supported. ↓
cause Using require() to load the plugin in a CommonJS context with v2/v3
fix
Use ESM import, or switch to dynamic import(). If you need CJS, downgrade to v1.x.
error TypeError: babelPluginUndebug is not a function ↓
cause Attempting to use a named export instead of default export
fix
Change import from
import { babelPluginUndebug } to import babelPluginUndebug Warnings
breaking Dropped support for Node 16 in v3.0.0 ↓
fix Upgrade to Node.js 18+ or use babel-plugin-undebug@^2.0.0
breaking Conversion to ESM-only in v2.0.0: require() is no longer supported ↓
fix Use ESM import or dynamic import(). If using CommonJS, pin to version 1.x.
deprecated TypeScript types now use @import JSDoc syntax (v3). May affect older TypeScript versions or tooling ↓
fix Update TypeScript to >=4.5 or configure skipLibCheck
gotcha Removes entire debug() call including side-effect expressions like debug(obj.x++) – be aware of side effects being dropped ↓
fix Refactor side effects out of debug arguments before running the plugin.
Install
npm install babel-plugin-undebug yarn add babel-plugin-undebug pnpm add babel-plugin-undebug Imports
- babelPluginUndebug wrong
import { babelPluginUndebug } from 'babel-plugin-undebug'correctimport babelPluginUndebug from 'babel-plugin-undebug' - babelPluginUndebug wrong
const babelPluginUndebug = require('babel-plugin-undebug')correctmodule.exports = { plugins: [['babel-plugin-undebug', options]] } - babel-plugin-undebug wrong
plugins: ['undebug']correctplugins: ['babel-plugin-undebug']
Quickstart
// Install: npm install --save-dev babel-plugin-undebug @babel/core @babel/cli
// babel.config.json
{
"plugins": ["babel-plugin-undebug"]
}
// example.js
const debug = require('debug')('myapp');
let count = 0;
debug('Count is %d', count++);
count++;
debug('Now count is %d', count);
console.log('Final', count);
// Run: npx babel example.js --out-file output.js
// output.js:
// let count = 0;
// count++;
// console.log('Final', count);
debug('Unreachable after removal');