Node

These options configure whether to polyfill or mock certain Node.js globals and modules. This allows code originally written for the Node.js environment to run in other environments like the browser.

This feature is provided by webpack's internal NodeStuffPlugin plugin. If the target is "web" (default) or "webworker", the NodeSourcePlugin plugin is also activated.

node

boolean = false object

This is an object where each property is the name of a Node global or module and each value may be one of the following...

  • true: Provide a polyfill.
  • 'mock': Provide a mock that implements the expected interface but has little or no functionality.
  • 'empty': Provide an empty object.
  • false: Provide nothing. Code that expects this object may crash with a ReferenceError. Code that attempts to import the module using require('modulename') may trigger a Cannot find module "modulename" error.

Not every Node global supports all four options. The compiler will throw an error for property-value combinations that aren't supported (e.g. process: 'empty'). See the sections below for more details.

These are the defaults:

module.exports = {
  //...
  node: {
    console: false,
    global: true,
    process: true,
    __filename: 'mock',
    __dirname: 'mock',
    Buffer: true,
    setImmediate: true

    // See "Other node core libraries" for additional options.
  }
};

Since webpack 3.0.0, the node option may be set to false to completely turn off the NodeStuffPlugin and NodeSourcePlugin plugins.

node.console

boolean = false string: 'mock'

The browser provides a console object with a very similar interface to the Node.js console, so a polyfill is generally not needed.

node.process

boolean = true string: 'mock'

node.global

boolean = true

See the source for the exact behavior of this object.

node.__filename

string = 'mock' boolean

Options:

  • true: The filename of the input file relative to the context option.
  • false: 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'.

node.__dirname

string = 'mock' boolean

Options:

  • true: The dirname of the input file relative to the context option.
  • false: The regular Node.js __dirname behavior. The dirname of the output file when run in a Node.js environment.
  • 'mock': The fixed value '/'.

node.Buffer

boolean = true string: 'mock'

node.setImmediate

boolean = true string: 'mock' | 'empty'

Other node core libraries

boolean string: 'mock' | 'empty'

This option is only activated (via NodeSourcePlugin) when the target is unspecified, "web" or "webworker".

Polyfills for Node.js core libraries from node-libs-browser are used if available, when the NodeSourcePlugin plugin is enabled. See the list of Node.js core libraries and their polyfills.

By default, webpack will polyfill each library if there is a known polyfill or do nothing if there is not one. In the latter case, webpack will behave as if the module name was configured with the false value.

To import a built-in module, use __non_webpack_require__, i.e. __non_webpack_require__('modulename') instead of require('modulename').

Example:

module.exports = {
  //...
  node: {
    dns: 'mock',
    fs: 'empty',
    path: true,
    url: false
  }
};

© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.
https://v4.webpack.js.org/configuration/node