Registry / devops / babel-plugin-transform-remove-imports

babel-plugin-transform-remove-imports

JSON →
library1.8.1jsnpmunverified

A Babel plugin to remove specific import/require declarations during transpilation. Current stable version is 1.8.1 (2024). Release cadence is irregular; updates address bug fixes and support for require(). Key differentiator: allows conditional removal of imports based on regex patterns, useful for SSR builds or tree-shaking. Small bundle size (~3KB). Peer dependency on @babel/core ^7.0.0. Ships TypeScript types.

npm install babel-plugin-transform-remove-imports
INSTALL
IMPORT
SIG · BABEL-PLUGIN-TRANS
B
babel-plugin-transform-remove-imports
devopsjavascriptv1.8.1
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-transform-remove-imports')
import plugin from 'babel-plugin-transform-remove-imports'
CommonJS is used as Babel plugins are typically configured via .babelrc or babel.config.js using require(). ESM default import is not supported in CJS context.
require('babel-plugin-transform-remove-imports')
const plugin = require('babel-plugin-transform-remove-imports')
const plugin = require('babel-plugin-transform-remove-imports').default
No default export; direct require() imports the plugin function.
PluginOptions
const { PluginOptions } = require('babel-plugin-transform-remove-imports/types')
TypeScript types are shipped but not documented. Use type import from 'babel-plugin-transform-remove-imports/types' only if types are needed.

Configures Babel to remove all imports matching pattern '^lodash(\\/.*)?$' (e.g., 'lodash' and 'lodash/merge').

// babel.config.js module.exports = { plugins: [ [ require('babel-plugin-transform-remove-imports'), { test: '^lodash(\\/.*)?$', // regex to match imports to remove } ] ] }; // Input: import _ from 'lodash'; import merge from 'lodash/merge'; import { map } from 'lodash'; import something from 'other-lib'; // Output: import something from 'other-lib';
Debug
Known issues
gotchaPlugin removes import statements without checking for side effects. If a module has side effects (e.g., polyfills), removing its import may break functionality.
fix
Explicitly keep required side-effect imports by adjusting the test regex or using a whitelist approach.
affects: >=1.0.0
gotchaThe `test` option is a string (regex pattern) or RegExp object. If passing a string, ensure proper escaping of backslashes in JSON/babel config (e.g., use '^lodash\/.*$' as string).
fix
Use double backslashes in JSON config strings: '^lodash\\/.*$'. Alternatively, use a RegExp object in JavaScript config.
affects: >=1.0.0
breakingRemoval of `require()` calls was introduced in v1.5.0. Before that, only ES import statements were removed.
fix
Upgrade to v1.5.0 or later if you need to remove CommonJS require() calls.
affects: <1.5.0
deprecatedNo deprecation warnings as of v1.8.1, but the plugin is not actively maintained (last release 2024, few updates). Consider using babel-plugin-import-remove or custom visitor if more features needed.
fix
Evaluate alternatives: babel-plugin-import-remove (more features) or implement a custom Babel visitor for precise control.
affects: >=1.0.0
gotchaPlugin removes both `import` and `require()` for the same module if pattern matches. This can accidentally remove dynamic require() calls if they match the pattern.
fix
Use a more specific test pattern to avoid catching dynamic requires. For example, if only static imports need removal, use a pattern that includes 'import' but not 'require'? Not directly supported; consider using regex negative lookahead.
affects: >=1.5.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'test')
Missing or misconfigured 'test' option in plugin config.
fix
Ensure the 'test' property is provided and is a string or RegExp. Example: { test: '^lodash$' }
SyntaxError: Invalid regular expression: /^lodash(\/.*)?$/: Unterminated group
Unescaped backslash in JSON config string.
fix
Double escape backslashes: '^lodash(\\/.*)?$'
Error: Requires Babel "^7.0.0-0" but was loaded with "0.0.0".
Babel version mismatch; plugin requires @babel/core 7.x.
fix
Update @babel/core to version 7 or later. Run: npm install @babel/core@^7.0.0
Upgrade
Version history
1.8.1latest on npm
Audit
Dependencies
@babel/corerequiredPeer dependency required for Babel plugin environment
Agent activity
8 hits · last 30 days
node
8
Resources
babel-plugin-transform-remove-imports — npm install babel-plugin-transform-remove-imports · libregistry