externals configuration option provides a way to exclude dependencies from the output bundles. Instead, the created bundle relies on that dependency to be present in the consumer’s environment.
Basic Configuration
webpack.config.js
Use Cases
- Libraries: When building a library, you don’t want to bundle dependencies
- CDN: Using dependencies from a CDN instead of bundling them
- Micro-frontends: Sharing dependencies between multiple apps
- Node.js: Excluding Node.js built-in modules
Externals Option
Define dependencies that should not be bundled.
String Format
webpack.config.js
Different Module Types
CommonJS
CommonJS2
AMD
UMD
Module (ESM)
Array Format
Externalize multiple modules:webpack.config.js
RegExp Format
Match modules with a regular expression:webpack.config.js
@company/* packages:
Function Format
Dynamically determine externals:webpack.config.js
Combined Formats
Use multiple formats together:webpack.config.js
Externals Type
Specify the default type for externals.Options:
'var', 'module', 'assign', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'amd', 'umd', 'jsonp', 'system', 'promise', 'import', 'script'webpack.config.js
Externals Presets
Enable presets for common external scenarios.
Node.js Preset
webpack.config.js
Web Preset
webpack.config.js
Common Patterns
Library Development
webpack.config.js
Node.js Application
Externalize all node_modules:webpack.config.js
webpack.config.js
CDN Dependencies
webpack.config.js
Micro-Frontends
webpack.config.js
Electron Application
webpack.config.js
Dynamic Import Externals
webpack.config.js
Layer-Specific Externals
webpack.config.js
Testing Externals
When testing, you may need to mock externals:webpack.test.js
jest.config.js