Skip to content

docs(config): Document how to use built-in Node.js modules #1368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions content/configuration/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ contributors:
- sokra
- skipjack
- oneforwonder
- Rob--W
---

These options configure whether to polyfill or mock certain [Node.js globals](https://nodejs.org/docs/latest/api/globals.html) 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`](https://github.com/webpack/webpack/blob/master/lib/NodeStuffPlugin.js).
These options configure whether to polyfill or mock certain [Node.js globals](https://nodejs.org/docs/latest/api/globals.html) 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`](https://github.com/webpack/webpack/blob/master/lib/NodeStuffPlugin.js) plugin. If the target is "web" (default) or "webworker", the [`NodeSourcePlugin`](https://github.com/webpack/webpack/blob/master/lib/node/NodeSourcePlugin.js) plugin is also activated.


## `node`
Expand All @@ -19,7 +22,7 @@ This is an object where each property is the name of a Node global or module and
- `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 to be defined may crash.
- `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.

W> 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.

Expand All @@ -34,10 +37,12 @@ node: {
__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 turn off the `NodeSourcePlugin` completely.
Since webpack 3.0.0, the `node` option may be set to `false` to completely turn off the `NodeStuffPlugin` and `NodeSourcePlugin` plugins.


## `node.console`
Expand Down Expand Up @@ -109,9 +114,13 @@ Default: `true`

`boolean | "mock" | "empty"`

Many other Node.js core libraries can be configured as well. See the list of [Node.js core libraries and their polyfills](https://github.com/webpack/node-libs-browser).
W> 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`](https://github.com/webpack/node-libs-browser) are used if available, when the `NodeSourcePlugin` plugin is enabled. See the list of [Node.js core libraries and their polyfills](https://github.com/webpack/node-libs-browser#readme).

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.

By default, Webpack will polyfill each library if there is a known polyfill or do nothing if there is not one.
T> To import a built-in module, use [`__non_webpack_require__`](/api/module-variables/#__non_webpack_require__-webpack-specific-), i.e. `__non_webpack_require__('modulename')` instead of `require('modulename')`.

Example:

Expand Down