Registry / devops / babel-plugin-remove-unused-vars

babel-plugin-remove-unused-vars

JSON →
library2.2.0jsnpmunverified

A Babel plugin that automatically removes unused variables, imports, function arguments, and destructured bindings from JavaScript code, acting as a Babel-based autofix for ESLint's no-unused-vars rule. As of version 2.2.0, it supports source-to-source transformations and is intended for use as a build tool to clean up code. The plugin runs via Babel CLI or programmatically and handles common cases but has known limitations with deep assignment expressions. It is released under the MIT license with moderate maintenance cadence on GitHub, positioned as a more aggressive alternative to manual lint fixes or tree-shaking.

npm install babel-plugin-remove-unused-vars
INSTALL
IMPORT
SIG · BABEL-PLUGIN-REMOV
B
babel-plugin-remove-unused-vars
devopsjavascriptv2.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
module.exports = require('babel-plugin-remove-unused-vars')
import plugin from 'babel-plugin-remove-unused-vars'
Plugin is CommonJS only; use require() in .babelrc or babel.config.js
PluginObj
const plugin = require('babel-plugin-remove-unused-vars')
import { PluginObj } from 'babel-plugin-remove-unused-vars'
No named exports; use default require
programmatic usage
const babel = require('@babel/core'); const plugin = require('babel-plugin-remove-unused-vars'); babel.transformSync(code, { plugins: [plugin] })
babel.transformSync(code, { plugins: ['babel-plugin-remove-unused-vars'] })
When using programmatically, pass the plugin function reference, not a string

Shows installation, configuration, CLI usage, and a simple transformation that removes unused imports and variables.

// Install: npm install --save-dev babel-plugin-remove-unused-vars // .babelrc { "plugins": ["babel-plugin-remove-unused-vars"] } // Or via CLI: // npx babel --no-babelrc --retain-lines --plugins babel-plugin-remove-unused-vars --out-dir dist/ src/ // Input: src/index.js import { unused } from './utils'; const a = 1; const b = a; console.log(b); // Output: dist/index.js import {} from './utils'; const b = 1; console.log(b);
Debug
Known issues
gotchaPlugin may produce false positives (removing actually used variables) or false negatives (keeping unused variables). Always review git diff after transformation.
fix
Run locally, inspect output with 'git diff', revert false removals manually.
affects: >=0.0.0
gotchaDeep assignment expressions (e.g., const a = foo(b = something())) are not fully handled. The right side may be removed even if the left side is used.
fix
Avoid deep assignments, or manually adjust after transformation.
affects: >=0.0.0
gotchaPlugin is intended for source-to-source transformation, not for minification or bundling. It should be run early in the build pipeline.
fix
Run before other transformations like minification.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'babel-plugin-remove-unused-vars'
Plugin not installed or not in node_modules.
fix
Run 'npm install --save-dev babel-plugin-remove-unused-vars' and ensure working directory has node_modules.
TypeError: babel.transformSync is not a function
Using a string plugin name instead of the plugin function reference when calling Babel programmatically.
fix
Use: babel.transformSync(code, { plugins: [require('babel-plugin-remove-unused-vars')] })
Unexpected token (1:0) when running Babel with --no-babelrc
Missing @babel/preset-env or other presets; --no-babelrc disables .babelrc but the plugin alone doesn't parse modern syntax.
fix
Add presets explicitly: npx babel --no-babelrc --presets @babel/preset-env --plugins babel-plugin-remove-unused-vars
Upgrade
Version history
2.2.0latest on npm
Audit
Dependencies
@babel/corerequiredRuntime dependency for Babel plugin execution
Agent activity
6 hits · last 30 days
node
6
Resources
babel-plugin-remove-unused-vars — npm install babel-plugin-remove-unused-vars · libregistry