Node
The following Node.js options configure whether to polyfill or mock certain Node.js globals.
This feature is provided by webpack's internal NodeStuffPlugin plugin.
node
false object
webpack.config.js
import path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default {
// ...
node: {
global: false,
__filename: false,
__dirname: false,
},
};The node option may be set to false to completely turn off the NodeStuffPlugin plugin.
node.global
boolean 'warn'
See the Node.js documentation for the exact behavior of this object.
Options:
true: Provide a polyfill or usingglobalThisif supported by your environment, see theenvironmentoption.false: Provide nothing. Code that expects this object may crash with aReferenceError.'warn': Show a warning when usingglobal.
node.__filename
boolean 'mock' | 'warn-mock' | 'node-module' | 'eval-only'
Options:
true: The filename of the input file relative to thecontextoption.false: Webpack won't touch your__filenameandimport.meta.filenamecode, which means you have the regular Node.js__filenameandimport.meta.filenamebehavior. The filename of the output file when run in a Node.js environment.'mock': The fixed value'/index.js'.'warn-mock': Use the fixed value of'/index.js'but show a warning.'node-module': Replace__filenamein CommonJS modules andimport.meta.filenamecode in ES modules tofileURLToPath(import.meta.url)whenoutput.moduleis enabled.'eval-only': Defer the resolution of__filename/import.meta.filenameto the Node.js runtime at execution time, but evaluate them in construction likerequire/importto properly resolve modules. Replace__filenamewithimport.meta.filenameand vice versa depending on theoutput.moduleoption (if your environment does not supportimport.meta.filename, the fallback will be used usingimport.meta.urlto get this value).
The default value can be affected by different target:
- Defaults to
'eval-only'iftargetis set to'node'or node-like environments (async-node,electron) or mixed targets (webandnodetogether). - Defaults to
'mock'iftargetis set to'web'or web-like environments.
node.__dirname
boolean 'mock' | 'warn-mock' | 'node-module' | 'eval-only'
Options:
true: The dirname of the input file relative to thecontextoption.false: Webpack won't touch your__dirnameandimport.meta.dirnamecode, which means you have the regular Node.js__dirnameandimport.meta.dirnamebehavior. The dirname of the output file when run in a Node.js environment.'mock': The fixed value'/'.'warn-mock': Use the fixed value of'/'but show a warning.'node-module': Replace__dirnamein CommonJS modules tofileURLToPath(import.meta.url + "/..")whenoutput.moduleis enabled.'eval-only': Defer the resolution of__dirname/import.meta.dirnameto the Node.js runtime at execution time, but evaluate them in construction likerequire/importto properly resolve modules. Replace__dirnamewithimport.meta.dirnameand vice versa depending on theoutput.moduleoption (if your environment does not supportimport.meta.filename, the fallback will be used usingimport.meta.urlto get this value).
The default value can be affected by different target:

