Skip to content

Commit c142e75

Browse files
authored
docs(configuration): update node page for webpack 5 (#4078)
* docs(configuration): update for webpack 5 * refine
1 parent 18e8e38 commit c142e75

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

src/content/configuration/node.md

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,18 @@ contributors:
99
- byzyk
1010
- EugeneHlushko
1111
- anikethsaha
12+
- chenxsan
1213
---
1314

14-
The following Node.js 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.
15+
The following Node.js options configure whether to polyfill or mock certain [Node.js globals](https://nodejs.org/docs/latest/api/globals.html).
1516

16-
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.
17+
This feature is provided by webpack's internal [`NodeStuffPlugin`](https://github.com/webpack/webpack/blob/master/lib/NodeStuffPlugin.js) plugin.
1718

19+
W> As of webpack 5, You can configure only `global`, `__filename` or `__dirname` under `node` option. If you're looking for how to polyfill `fs` alike in Node.js under webpack 5, please check [resolve.fallback](/configuration/resolve/#resolvefallback) for help.
1820

1921
## `node`
2022

21-
`boolean = false` `object`
22-
23-
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...
24-
25-
- `true`: Provide a polyfill.
26-
- `'mock'`: Provide a mock that implements the expected interface but has little or no functionality.
27-
- `'empty'`: Provide an empty object.
28-
- `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.
29-
30-
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. `global: 'empty'`). See the sections below for more details.
31-
32-
T> If you are using a module which needs global variables in it, use `ProvidePlugin` instead of `global`.
33-
34-
These are the defaults:
23+
`boolean: false` `object`
3524

3625
__webpack.config.js__
3726

@@ -46,38 +35,40 @@ module.exports = {
4635
};
4736
```
4837

49-
Since webpack 3.0.0, the `node` option may be set to `false` to completely turn off the `NodeStuffPlugin` and `NodeSourcePlugin` plugins.
38+
Since webpack 3.0.0, the `node` option may be set to `false` to completely turn off the `NodeStuffPlugin` plugin.
5039

5140
## `node.global`
5241

53-
`boolean = true`
42+
`boolean`
5443

55-
Defaults to `false` for [targets](/configuration/target/) `node`, `async-node` and `electron-main`.
44+
T> If you are using a module which needs global variables in it, use `ProvidePlugin` instead of `global`.
5645

57-
See [the source](https://nodejs.org/api/globals.html) for the exact behavior of this object.
46+
See [the Node.js documentation](https://nodejs.org/api/globals.html#globals_global) for the exact behavior of this object.
5847

48+
Options:
5949

60-
## `node.__filename`
50+
- `true`: Provide a polyfill.
51+
- `false`: Provide nothing. Code that expects this object may crash with a `ReferenceError`.
6152

62-
`boolean` `string = mock`
53+
## `node.__filename`
6354

64-
Defaults to `false` for [targets](/configuration/target/) `node`, `async-node` and `electron-main`.
55+
`boolean` `string: 'mock' | 'eval-only'`
6556

6657
Options:
6758

6859
- `true`: The filename of the __input__ file relative to the [`context` option](/configuration/entry-context/#context).
69-
- `false`: The regular Node.js `__filename` behavior. The filename of the __output__ file when run in a Node.js environment.
70-
- `'mock'`: The fixed value `'index.js'`.
60+
- `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.
61+
- `'mock'`: The fixed value `'/index.js'`.
62+
- `'eval-only'`
7163

7264

7365
## `node.__dirname`
7466

75-
`boolean` `string = mock`
76-
77-
Defaults to `false` for [targets](/configuration/target/) `node`, `async-node` and `electron-main`.
67+
`boolean` `string: 'mock' | 'eval-only'`
7868

7969
Options:
8070

8171
- `true`: The dirname of the __input__ file relative to the [`context` option](/configuration/entry-context/#context).
82-
- `false`: The regular Node.js `__dirname` behavior. The dirname of the __output__ file when run in a Node.js environment.
72+
- `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.
8373
- `'mock'`: The fixed value `'/'`.
74+
- `'eval-only'`

0 commit comments

Comments
 (0)