Documentation Index
Fetch the complete documentation index at: https://docs.webpack.js.org/llms.txt
Use this file to discover all available pages before exploring further.
The 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
module.exports = {
externals: {
react: 'React',
jquery: 'jQuery'
}
};
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
externals
string | object | function | RegExp | array
Define dependencies that should not be bundled.externals: {
lodash: 'lodash',
react: 'React'
}
module.exports = {
externals: {
// Property: Module to exclude
// Value: Global variable name
react: 'React',
'react-dom': 'ReactDOM',
jquery: 'jQuery',
lodash: '_'
}
};
In your code:
import React from 'react'; // Uses global window.React
import $ from 'jquery'; // Uses global window.jQuery
Different Module Types
CommonJS
externals: {
react: 'commonjs react' // require('react')
}
CommonJS2
externals: {
react: 'commonjs2 react' // module.exports = require('react')
}
AMD
externals: {
react: 'amd react' // define(['react'], ...)
}
UMD
externals: {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
}
}
Module (ESM)
externals: {
react: 'module react' // import from 'react'
}
Externalize multiple modules:
module.exports = {
externals: ['react', 'react-dom', 'lodash']
};
This assumes the global variable name matches the module name.
Match modules with a regular expression:
module.exports = {
externals: /^(@company\/.+|lodash)$/
};
Example: Externalize all @company/* packages:
Dynamically determine externals:
module.exports = {
externals: function({ context, request }, callback) {
// Externalize all modules in node_modules
if (/^[a-z\-0-9]+$/.test(request)) {
return callback(null, 'commonjs ' + request);
}
// Not an external
callback();
}
};
Async function:
externals: async function({ context, request }) {
if (request === 'special-module') {
return 'commonjs special-module';
}
}
Use multiple formats together:
module.exports = {
externals: [
{
// Specific modules
react: 'React',
lodash: '_'
},
// All modules matching pattern
/^@company\//,
// Dynamic function
function({ request }, callback) {
if (request.startsWith('legacy-')) {
return callback(null, 'var ' + request);
}
callback();
}
]
};
Externals Type
Specify the default type for externals.externalsType: 'commonjs' // or 'var', 'module', 'umd', etc.
Options: 'var', 'module', 'assign', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'amd', 'umd', 'jsonp', 'system', 'promise', 'import', 'script'
module.exports = {
externalsType: 'commonjs',
externals: {
react: 'react' // Treated as 'commonjs react'
}
};
Externals Presets
Enable presets for common external scenarios.externalsPresets: {
node: true, // Externalize Node.js built-ins
web: true, // Externalize http(s):// imports
electron: true,
electronMain: true,
electronPreload: true,
electronRenderer: true
}
Node.js Preset
module.exports = {
externalsPresets: {
node: true // Treats node built-ins as externals
}
};
Web Preset
module.exports = {
externalsPresets: {
web: true // Treats http(s):// and std: imports as externals
}
};
Common Patterns
Library Development
module.exports = {
externals: {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
}
},
output: {
library: {
name: 'MyLibrary',
type: 'umd'
}
}
};
Node.js Application
Externalize all node_modules:
const nodeExternals = require('webpack-node-externals');
module.exports = {
target: 'node',
externalsPresets: { node: true },
externals: [nodeExternals()]
};
Manual approach:
const fs = require('fs');
module.exports = {
target: 'node',
externals: [
function ({ context, request }, callback) {
if (/node_modules/.test(context)) {
return callback(null, 'commonjs ' + request);
}
callback();
}
]
};
CDN Dependencies
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
externals: {
react: 'React',
'react-dom': 'ReactDOM'
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html',
cdn: {
js: [
'https://unpkg.com/react@18/umd/react.production.min.js',
'https://unpkg.com/react-dom@18/umd/react-dom.production.min.js'
]
}
})
]
};
HTML template:
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
</body>
</html>
Micro-Frontends
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
module.exports = {
plugins: [
new ModuleFederationPlugin({
name: 'app1',
shared: {
react: { singleton: true },
'react-dom': { singleton: true }
}
})
],
externals: {
// Additional externals if needed
}
};
Electron Application
module.exports = {
target: 'electron-renderer',
externalsPresets: {
electron: true,
electronRenderer: true
},
externals: {
// Additional electron modules
'electron-store': 'commonjs electron-store'
}
};
Dynamic Import Externals
module.exports = {
externalsType: 'import',
externals: {
'big-library': 'https://cdn.example.com/big-library.js'
}
};
Usage:
import('big-library').then(lib => {
// Use library
});
Layer-Specific Externals
module.exports = {
externals: {
byLayer: {
'client': {
react: 'React'
},
'server': {
express: 'commonjs express'
}
}
}
};
Testing Externals
When testing, you may need to mock externals:
module.exports = {
externals: {
// No externals for tests - bundle everything
}
};
Or use Jest configuration:
module.exports = {
moduleNameMapper: {
'^react$': '<rootDir>/node_modules/react'
}
};
Troubleshooting
External Not Found
Make sure the external is available in the runtime environment:
<!-- Include before your bundle -->
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script src="your-bundle.js"></script>
Wrong Global Name
Check the library’s documentation for the correct global variable name:
// Wrong
externals: { react: 'react' }
// Correct
externals: { react: 'React' }
TypeScript Externals
Install type definitions for externalized packages:
npm install --save-dev @types/react @types/react-dom