babel-plugin-remove-webpack

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

Babel plugin that removes webpack-specific functions (require.ensure, require.include) from JavaScript code, useful for running universal JavaScript on the server without shims. Version 1.1.0 is the latest and only stable release, with low maintenance cadence (last update years ago). Unlike manual polyfills, this plugin automatically strips webpack code at the AST level, enabling server-side execution without code splitting. For Node.js environments with babel-register, it avoids defining dummy functions per file. Not for use in webpack builds, as it would disable code splitting.

error Cannot find module 'babel-plugin-remove-webpack'
cause Missing npm install or wrong working directory.
fix
Run 'npm install babel-plugin-remove-webpack --save-dev' and ensure node_modules is accessible.
error TypeError: Cannot read property 'ensure' of undefined
cause Plugin not applied or not working; require.ensure remains in code.
fix
Check Babel config includes 'remove-webpack' in plugins, and that the transform runs before execution.
gotcha Do not use this plugin in webpack builds; it will remove code splitting.
fix Only apply in server-side Babel transforms, not in webpack configuration.
deprecated require.ensure is deprecated in webpack in favor of dynamic import(). This plugin does not handle dynamic imports.
fix Use dynamic import() on modern webpack and consider a different plugin for that case.
gotcha Plugin is CJS-only; no ESM export. Cannot import via import statement.
fix Use require() in Babel config or transform.
npm install babel-plugin-remove-webpack
yarn add babel-plugin-remove-webpack
pnpm add babel-plugin-remove-webpack

Demonstrates how to install, configure, and use the plugin with babel-register or programmatically.

// Install: npm install --save-dev babel-plugin-remove-webpack
// In .babelrc or babel.config.js:
{
  "plugins": ["remove-webpack"]
}

// With babel-register in your server entry:
require('@babel/register')({ plugins: ['remove-webpack'] });
require('./client-code'); // will run without require.ensure/require.include

// Or programmatically:
const babel = require('@babel/core');
const result = babel.transform(
  `require.ensure(['a'], function(r) { r('a'); });`,
  { plugins: ['remove-webpack'] }
);
console.log(result.code); // (function () { require('a'); })();