Skip to content

Commit 6193e8c

Browse files
committed
Work around Node environment variable bug
Work around nodejs/node#18463 by detecting the conditions under which the bug occurs and performing a simple operation that resets the error state if necessary.
1 parent e4375f8 commit 6193e8c

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lib/EnvironmentPlugin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
const DefinePlugin = require("./DefinePlugin");
99

10+
const needsEnvVarFix = ["8", "9"].includes(process.versions.node.split(".")[0]) &&
11+
process.platform === "win32";
12+
1013
class EnvironmentPlugin {
1114
constructor(keys) {
1215
if(Array.isArray(keys)) {
@@ -23,6 +26,12 @@ class EnvironmentPlugin {
2326

2427
apply(compiler) {
2528
const definitions = this.keys.reduce((defs, key) => {
29+
// Work around https://github.com/nodejs/node/pull/18463,
30+
// affecting Node 8 & 9 by performing an OS-level
31+
// operation that always succeeds before reading
32+
// environment variables:
33+
if(needsEnvVarFix) require("os").cpus();
34+
2635
const value = process.env[key] !== undefined ? process.env[key] : this.defaultValues[key];
2736

2837
if(value === undefined) {

test/configCases/plugins/environment-plugin/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ it("should import multiple process.env var with default values", () => {
3030
});
3131

3232
it("should import process.env var with empty value", () => {
33+
process.env.PATH;
3334
if(process.env.III !== "")
3435
require.include("iii");
3536
});

0 commit comments

Comments
 (0)