babel-plugin-remove-import-export

raw JSON →
1.1.1 verified Sat Apr 25 auth: no javascript maintenance

A Babel plugin that strips import and export declarations from source files, useful for preparing code snippets for platforms like LeetCode. Current stable version is 1.1.1 (last updated 2020). It is lightweight and simple, with no dependencies. Options allow selective removal of imports, exports, or default exports, and control over preserving named export declarations. Unlike general code cleanup tools, it specifically targets module syntax only.

error Error: Cannot find module 'babel-plugin-remove-import-export'
cause Plugin not installed or babel config refers to wrong name.
fix
Run: npm install --save-dev babel-plugin-remove-import-export. Ensure .babelrc uses 'remove-import-export' (not the full name).
error TypeError: Cannot read property 'removeImport' of undefined
cause Options passed in wrong format (array spread instead of nested array).
fix
In .babelrc, use: "plugins": [["remove-import-export", { "removeImport": false }]]
error The plugin generated some unexpected output (import/export still present)
cause Options not recognized (typo in option name) or plugin not applied correctly.
fix
Verify option spelling; e.g., 'preseveNamedDeclaration' not 'preserveNamedDeclaration'. Also check .babelrc correct syntax.
gotcha Preserving named export declaration with preseveNamedDeclaration (sic) misspelled option. The correct option is 'preseveNamedDeclaration' (typo preserved for compatibility).
fix Use option name exactly as spelled: 'preseveNamedDeclaration' (not 'preserveNamedDeclaration').
gotcha Removing imports may cause runtime errors if the code depends on those imports. The plugin does not check for usage.
fix Manually ensure removed imports are not used in the remaining code, or use a bundler/linter to detect dangling references.
breaking Default options remove all imports and exports (including default exports) unless overridden. Some users expect default exports to be preserved.
fix Set 'removeExportDefault' to false in options to preserve default export.
gotcha The plugin does not handle re-exports (e.g., export { x } from 'module') correctly; it may leave partial code.
fix Avoid using with re-export statements, or test output manually.
npm install babel-plugin-remove-import-export
yarn add babel-plugin-remove-import-export
pnpm add babel-plugin-remove-import-export

Demonstrates basic usage: removing import/export declarations from a source file, preserving only the body.

// Install: npm install --save-dev babel-plugin-remove-import-export
// .babelrc
{
  "plugins": ["remove-import-export"]
}
// Input
import { LinkedList } from 'some-lib';
function foo() { return new LinkedList(['bar']); }
export class Solution { add(a,b) { return a+b; } }
export default foo;
// Output
function foo() { return new LinkedList(['bar']); }
class Solution { add(a,b) { return a+b; } }