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.
As of webpack 5, You can configure only
global,__filenameor__dirnameundernodeoption. If you're looking for how to polyfillfsalike in Node.js under webpack 5, please check resolve.fallback for help.
nodeboolean: false object
webpack.config.js
module.exports = {
//...
node: {
global: false,
__filename: false,
__dirname: false,
}
}; Since webpack 3.0.0, the node option may be set to false to completely turn off the NodeStuffPlugin plugin.
node.globalboolean
If you are using a module which needs global variables in it, use
ProvidePlugininstead ofglobal.
See the Node.js documentation for the exact behavior of this object.
Options:
true: Provide a polyfill.false: Provide nothing. Code that expects this object may crash with a ReferenceError.node.__filenameboolean string: 'mock' | 'eval-only'
Options:
true: The filename of the input file relative to the context option.false: Webpack won't touch your __filename code, which means you have the regular Node.js __filename behavior. The filename of the output file when run in a Node.js environment.'mock': The fixed value '/index.js'.'eval-only'node.__dirnameboolean string: 'mock' | 'eval-only'
Options:
true: The dirname of the input file relative to the context option.false: Webpack won't touch your __dirname code, which means you have the regular Node.js __dirname behavior. The dirname of the output file when run in a Node.js environment.'mock': The fixed value '/'.'eval-only'