New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing environment variables in client-side code and creating builds for multiple environments #1250
Comments
|
I'm not sure I fully understand your requirements, but if you are wanting a GLOBAL variable available in all environments, maybe the webpack DefinePlugin could help! If you add your own variable to the the webpack.base.babel.js it will be available on all platforms. In the past I've added the API URL to If you want to keep the linter happy you can add a globals sections to the eslint config, which is in Please let us know if that helps or if I've completely missed your question :) |
|
Thanks, but it's not exactly what I'm looking for. I've actually tried that approach, but it requires defining the API_URL as a fixed string constant, and not as an environment variable. Let me illustrate what I'm trying to achieve:
I've read through the documentation and previously closed issues, but I can't seem to find any similar way of implementing this approach when deploying the boilerplate to multiple server environments. |
|
Ah okay, thanks for clarifying. I've just done a little experimenting with Steps I followed:
Then I tested with both Give that a try and let us know if it works? Likewise, if you do find a better solution or ways to improve this one, please keep us informed. |
|
Thanks, this method works on my local machine. My only issue now is that when I deploy this update to Heroku, the |
|
Firstly I do not know anything about Heroku so please take it easy on me if I am wrong. The Heroku does seem to have environment variables tho, which should be available to |
|
Thanks @samit4me, no problem, I understand. Your suggestion was very helpful and I was able to use your suggested implementation to make loading of environment variables work in my So here's what I did to make it work:
This might not be be the best implementation for creating builds for multiple environments in Heroku, but this one will do for now. I'm pretty sure a lot of users of this boilerplate also use 'Heroku Pipelines', and I hope there's a cleaner way of using Thank you very much @samit4me for the help 👍 |
|
That's great @karloespiritu, I'm really glad you got something that works for you. I agree, this certainly is a common use case and maybe something could be added to the docs in the future, but for now I think this issue is a fantastic reference for anyone trying to do something similar. Thank you for sharing your solution, I am certain it will help someone out in the future 👌 |
|
I was looking for a solution for multiple environment with this boilerplate and this looks just right! Sadly I got an error right at the start :'( So, what I've did was: any suggestions to solve this dependency issue? |
|
@dcarneiro |
|
@gihrig, indeed it does. I've panicked way to soon. Thanks for the help |
|
Hi @samit4me sorry for replying to an old and closed issue, but would you mind to share the content of that |
|
To be honest @xavifuefer I cannot remember but I can only guess that it was something like this What are you trying to acheive and what is your environment? |
|
i've been wrestling with this as well! i got it to work by adding |
|
The above works great, but assumes you're deploying your entire codebase to the production server and running "npm run build" from the prod server. Has anyone found a way to only deploy the /build folder and package.json to the server and still have environment variables "passed" to the client-side? |
|
Anyone figured out how to bypass a lint error for undefined variables? |
|
@tplummerfni In eslint, if you leave a comment such as |
|
@rsteckler are you talking about some runtime variables? If you are I have been able to do so by including a script tag inside |
|
@benjaminshafii For those who might have less context:
Love it - Thanks, Ben! |
|
Do all these methods require you to build on the server? Is that good practice? |
|
I'm currently having problems with setting environment variables for the client side code after the build stage as well. Typically, I'm deploying multiple versions of the app with one image and was trying to avoid building multiple docker images. So the only available option here would be passing the parameters via |
|
@zhenyulin |
|
@rsteckler I endup passing the env-var to the client side script tag with the initial page rendering on the server, it might be a good idea to use tools like |
|
For future reference, this is now a feature of
|
|
I had the same exact issue. Using React + Webpack on a 100% client based application (frontend). I was using dotenv-webpack to manage environment variables. These were working totally fine on development but not on production. In my case, I made the mistake of copying and pasting the code from the dotenv-webpack library. All I had to do was include BEFORE: AFTER: -Notice that I deleted However, it works like a charm now! |
|
Since I needed the env variables to be fetched during runtime and the solutions here all seemed to set the env variables during build I came up with the following "hack" in my ´_document.js´: For me So far this does the job for me. UPDATE And then instead of Also I noticed that the JS modules bundeled by Next/Webpack seem to have a local variable called |
|
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I'm having issues accessing environment variables (i.e API_URL) after I deploy and build my app to multiple environments (staging, production, etc).
I'm using the boilerplate as a client application that consumes data from a REST API. My application is deployed in Heroku and I use Heroku Pipelines for my deployment and build process.
What's the best way to access environment variables after I build the boilerplate (
npm run build)? I deploy the boilerplate to multiple environments (staging, production, etc.) with different environment variables, and I can't seem to find a preferred of doing this.Thank you very much.
The text was updated successfully, but these errors were encountered: