peer-deps-externals-webpack-plugin

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

Webpack plugin (v1.0.4) that automatically adds a library's peerDependencies to webpack's externals configuration, preventing them from being bundled. This keeps bundle size minimal and avoids duplicate dependencies when the library is consumed. Requires webpack >=2.0.0. A lightweight alternative to manually listing externals or using other dependency exclusion plugins. Stable with no active development.

error Module not found: Error: Can't resolve 'react'
cause React is a peerDependency but not excluded from the bundle because PeerDepsExternalsPlugin is not configured.
fix
Add new PeerDepsExternalsPlugin() to webpack plugins array.
error TypeError: PeerDepsExternalsPlugin is not a constructor
cause Importing the plugin incorrectly (e.g., using named import when it's a default export).
fix
Use const PeerDepsExternalsPlugin = require('peer-deps-externals-webpack-plugin');
error ./node_modules/.bin/webpack: line 1: unexpected EOF while looking for matching ``'
cause Shell quoting issue when peerDependencies contain environment variable syntax (e.g., ${npm_package_peerDependencies}) in older versions.
fix
Upgrade webpack and the plugin to latest versions; or avoid using shell variables in peerDependencies.
gotcha Plugin only reads peerDependencies from the nearest package.json. If a library is nested (e.g., in monorepo), externals may be incorrect.
fix Ensure that the webpack process is run in the directory where package.json with peerDependencies resides, or set context in webpack config.
gotcha Plugin uses all peerDependencies from package.json; it does not filter based on environment (e.g., react vs react-dom for browser).
fix Manually edit externals or use a function for fine-grained externals control if needed.
deprecated Plugin is no longer actively maintained; last release in 2021. May not support webpack 5+ features like module federation.
fix Consider alternatives like webpack-node-externals or manual externals configuration.
npm install peer-deps-externals-webpack-plugin
yarn add peer-deps-externals-webpack-plugin
pnpm add peer-deps-externals-webpack-plugin

Configures webpack to automatically exclude peer dependencies from the bundle using PeerDepsExternalsPlugin.

// webpack.config.js
const PeerDepsExternalsPlugin = require('peer-deps-externals-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: __dirname + '/dist',
    filename: 'bundle.js',
    libraryTarget: 'commonjs2', // or 'umd' for libraries
  },
  plugins: [
    new PeerDepsExternalsPlugin(),
  ],
};