You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/configuration/node.md
+20-29Lines changed: 20 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -9,29 +9,18 @@ contributors:
9
9
- byzyk
10
10
- EugeneHlushko
11
11
- anikethsaha
12
+
- chenxsan
12
13
---
13
14
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).
15
16
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.
17
18
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.
18
20
19
21
## `node`
20
22
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`
35
24
36
25
__webpack.config.js__
37
26
@@ -46,38 +35,40 @@ module.exports = {
46
35
};
47
36
```
48
37
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.
50
39
51
40
## `node.global`
52
41
53
-
`boolean = true`
42
+
`boolean`
54
43
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`.
56
45
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.
58
47
48
+
Options:
59
49
60
-
## `node.__filename`
50
+
-`true`: Provide a polyfill.
51
+
-`false`: Provide nothing. Code that expects this object may crash with a `ReferenceError`.
61
52
62
-
`boolean``string = mock`
53
+
## `node.__filename`
63
54
64
-
Defaults to `false` for [targets](/configuration/target/)`node`, `async-node` and `electron-main`.
55
+
`boolean``string: 'mock' | 'eval-only'`
65
56
66
57
Options:
67
58
68
59
-`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'`
71
63
72
64
73
65
## `node.__dirname`
74
66
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'`
78
68
79
69
Options:
80
70
81
71
-`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.
0 commit comments