node option configures whether to polyfill or mock certain Node.js globals and modules. This is useful when bundling code that uses Node.js APIs for the browser.
Basic Configuration
webpack.config.js
Node Options
Configure Node.js polyfills.
Available Options
__dirname
Configure
__dirname variable behavior.true: Provide the directory name of the module filefalse: Use Node.js__dirnamevalue (path to the webpack output)'mock': Provide'/'as value'warn-mock': Provide'/'with a warning'eval-only': Only available in eval-based devtools
false (webpack 5)__filename
Configure
__filename variable behavior.true: Provide the filename of the module filefalse: Use Node.js__filenamevalue'mock': Provide'/index.js'as value'warn-mock': Provide'/index.js'with a warning'eval-only': Only available in eval-based devtools
false (webpack 5)global
Configure
global variable behavior.true: Provide a polyfillfalse: Do not provide a polyfill'warn': Show a warning whenglobalis used
false (webpack 5)Webpack 5 Changes
Webpack 5 stopped providing Node.js polyfills by default. You need to:- Configure node option to control built-in globals
- Use resolve.fallback for Node.js core modules
- Install polyfill packages manually
Migration from Webpack 4
Webpack 4 (Automatic Polyfills)
Webpack 5 (Manual Configuration)
Common Configurations
Browser Application (No Polyfills)
webpack.config.js
Browser with Node.js Globals
webpack.config.js
Electron Application
webpack.config.js
Node.js Application
webpack.config.js
Polyfill Packages
Core Modules
Configuration with Polyfills
webpack.config.js
ProvidePlugin for Globals
webpack.config.js
global-shim.js:
global-shim.js
Handling Specific Modules
Buffer Usage
Process Usage
Crypto Usage
Disable Specific Modules
When a module is not available in browser:webpack.config.js