Skip to content

Commit 1fdd547

Browse files
Rob--Wskipjack
authored andcommitted
docs(config): improve node documentation (#1368)
The documentation about using built-in Node.js modules was very poor (i.e. non-existent). This expands the documentation of the "node" configuration option, and shows how one can require built-in modules if desired. Furthermore, all possible effects of the options are explicitly documented. Instead of being vague of what happens when `false` is used, it is explicitly spelled out what happens. References: - https://github.com/webpack/webpack/blob/a589a6c9789a9d342fc630e36ab81827dd20289b/lib/WebpackOptionsApply.js shows when the NodeStuffPlugin and NodeSourcePlugin plugins are used. - https://github.com/webpack/webpack/blob/a589a6c9789a9d342fc630e36ab81827dd20289b/lib/NodeStuffPlugin.js is the plugin that is used for every target. - https://github.com/webpack/webpack/blob/a589a6c9789a9d342fc630e36ab81827dd20289b/lib/node/NodeSourcePlugin.js is the plugin that is only used for "web" and "webworker" targets. - https://github.com/webpack/webpack/blob/a589a6c9789a9d342fc630e36ab81827dd20289b/lib/MultiModule.js#L65-L67 is the generated "webpackMissingModule" code for unresolved modules. (when the NodeSourcePlugin is not used, the environment's "require" is used. In Node.js, this also throws a "Cannot find module 'modulename'" error (with single quotes instead of double quotes)).
1 parent d6f9075 commit 1fdd547

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

content/configuration/node.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ contributors:
55
- sokra
66
- skipjack
77
- oneforwonder
8+
- Rob--W
89
---
910

10-
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).
11+
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.
12+
13+
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.
1114

1215

1316
## `node`
@@ -19,7 +22,7 @@ This is an object where each property is the name of a Node global or module and
1922
- `true`: Provide a polyfill.
2023
- `"mock"`: Provide a mock that implements the expected interface but has little or no functionality.
2124
- `"empty"`: Provide an empty object.
22-
- `false`: Provide nothing. Code that expects this object to be defined may crash.
25+
- `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.
2326

2427
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.
2528

@@ -34,10 +37,12 @@ node: {
3437
__dirname: "mock",
3538
Buffer: true,
3639
setImmediate: true
40+
41+
// See "Other node core libraries" for additional options.
3742
}
3843
```
3944

40-
Since webpack 3.0.0, the `node` option may be set to `false` to turn off the `NodeSourcePlugin` completely.
45+
Since webpack 3.0.0, the `node` option may be set to `false` to completely turn off the `NodeStuffPlugin` and `NodeSourcePlugin` plugins.
4146

4247

4348
## `node.console`
@@ -109,9 +114,13 @@ Default: `true`
109114

110115
`boolean | "mock" | "empty"`
111116

112-
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).
117+
W> This option is only activated (via `NodeSourcePlugin`) when the target is unspecified, "web" or "webworker".
118+
119+
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).
120+
121+
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.
113122

114-
By default, Webpack will polyfill each library if there is a known polyfill or do nothing if there is not one.
123+
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')`.
115124

116125
Example:
117126

0 commit comments

Comments
 (0)