Build-time environment variables for web apps
See original GitHub issueDesired Behavior
It would be great if there was a built-in way to expose build-time environment variables to web/React/Angular projects.
Current Behavior
process.env is not exposed, so you have to jump through some hoops and craft a custom solution (e.g. copying files around in a build script, using babel-plugin-preval which comes with babel-loader caching issues, etc.) to read environment variables in web projects (as far as I can tell, anyway).
Other
Prior art for this exists in other popular web frameworks/libraries. create-react-app automatically embeds any environment variable prefixed with REACT_APP_ and Next.js includes an env key in the main config file (evaluated during build) that you can populate as you see fit.
Would either of these approaches be appropriate for Nx, or is there another way that would be even better? Or does an idiomatic way already exist?
Thanks.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:9 (2 by maintainers)

Top Related StackOverflow Question
I just lost over 2 hours of time because of the horrible implementation of this feature.
Build time variables MUST NOT be conflated with environment variables: they are simply not the same. It is unacceptable that
process.envbe replaced during build-time, becauseprocess.envis per definition runtime, i.e. in fact there even does not exist aprocessat build time.If you want to replace variables at build time, this should be done through another api. Be it
buildvars.MY_VARor something else - I don’t care, but right now it is just plain confusing. People even go as far as to recommendprocess.env["NODE" + "_ENV"]to circumvent the nx compiler because of this conflated idea.Apart from this behavior being counter-intuitive, it can also lead to security issues:
Thirdly, it is also directly opposing other, established build configurations: for instance, converting a plain
typescript/node.jsapplication into an NX build might now lead to different behavior at runtime, because of unexpected compile-time code replacements.Therefore, I really, really think the replacement of
process.envat build time should be revisited. And I believe I’m not the only one, considering there are over 3 stackoverflow posts of confused people discussing this behavior.@wKovacs64 @jdpearce @EranSch
We at Blackberry started working on this feature to be part of the framework.