From ac015f613b3f9883376d1dbcb3c7596c81487fbe Mon Sep 17 00:00:00 2001 From: Sebastian Werner Date: Fri, 5 Apr 2019 14:15:34 +0200 Subject: [PATCH] Added protection against non-writable process.env environments e.g. inside Webpack bundles --- src/index.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index b986436..52fa046 100644 --- a/src/index.js +++ b/src/index.js @@ -43,12 +43,21 @@ dotenvFiles.forEach((dotenvFile) => { }) if (process.env.APP_ROOT == null) { - process.env.APP_ROOT = appRoot.get() + const detectedRoot = appRoot.get() + try { + process.env.APP_ROOT = detectedRoot + } catch (error) { + throw new Error( + "Universal-DotEnv requires a writable process.env! Please make sure that this code is not transpiled with Webpack." + ) + } } if (process.env.APP_SOURCE == null) { const sourceFolder = path.join(process.env.APP_ROOT, "src") - process.env.APP_SOURCE = fs.existsSync(sourceFolder) ? sourceFolder : process.env.APP_ROOT + process.env.APP_SOURCE = fs.existsSync(sourceFolder) ? + sourceFolder : + process.env.APP_ROOT } // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be