Node

The following Node.js 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. global: 'empty'). See the sections below for more details.

If you are using a module which needs global variables in it, use ProvidePlugin instead of global.

These are the defaults:

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 and NodeSourcePlugin plugins.

node.global

boolean = true

Defaults to false for targets node, async-node and electron-main.

See the source for the exact behavior of this object.

node.__filename

boolean string = mock

Defaults to false for targets node, async-node and electron-main.

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

boolean string = mock

Defaults to false for targets node, async-node and electron-main.

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 '/'.