Skip to content

Commit

Permalink
Work around Node environment variable bug
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
addaleax committed Jan 30, 2018
1 parent e4375f8 commit 8da8b93
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/EnvironmentPlugin.js
Expand Up @@ -7,6 +7,9 @@

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

const needsEnvVarFix = ["8", "9"].indexOf(process.versions.node.split(".")[0]) >= 0 &&
process.platform === "win32";

class EnvironmentPlugin {
constructor(keys) {
if(Array.isArray(keys)) {
Expand All @@ -23,6 +26,13 @@ class EnvironmentPlugin {

apply(compiler) {
const definitions = this.keys.reduce((defs, key) => {
// TODO remove once the fix has made its way into Node 8.
// Work around https://github.com/nodejs/node/pull/18463,
// affecting Node 8 & 9 by performing an OS-level
// operation that always succeeds before reading
// environment variables:
if(needsEnvVarFix) require("os").cpus();

const value = process.env[key] !== undefined ? process.env[key] : this.defaultValues[key];

if(value === undefined) {
Expand Down

0 comments on commit 8da8b93

Please sign in to comment.