-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Setting environment variables for client side #6533
Comments
@karinchechik I'm not sure I follow, are the environment variables valid in your Note: the |
@ijjk It happens in every page, for example: In getInitialProps I have a fetch that uses process.env.API_TOKEN that displays the data as it should (but only after refreshing and not after a click from the index). |
@karinchechik Thanks for the repo! Are you trying to provide your data as environment variables or using a If you're just using environment variables and not a During build Next.js will use the If you need these values to be able to change without rebuilding and you aren't using serverless mode you can use the I can submit a PR with an updated config if this doesn't help. |
Thank you so much!! I tried the webpack option (which didn't work) before the env in next.config.js option and I didn't remove the dotenv-webpack. Now I used the Thanks again :) |
https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration publicRuntimeConfig can be used for this, as per https://whitefield.dev/blog/nextjs-env-variables |
The made it really easy to expose environment variables to the browser:
|
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
After deploying to Heroku:
At the index - First load returns the data I fetched in getInitialProps, but in a regular function I get an error message because the env vars are missing.
When I go to a different page I get the same error, but when I refresh I can see the data fetched at getInitialProps. But again, in a regular function I get an error for the missing env variables.
Locally it works. I tried dotenv-webpack but it doesn't help. I added the config vars in Heroku.
The code uses the environment variables on server side rendering (first load of index and in general, every page after refresh), so the problem is probably not the way I set the config vars. On a regular function (triggered by a button click), I get a message for the missing variables and also after I navigate to a different page.
This is my attemps in next.config.js:
First option that failed:
Second option that failed:
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
You can see it here - https://furrytales.herokuapp.com/
Expected behavior
Local version is able to use environment variable on the client side from .env file.
Screenshots
You can see it here - https://furrytales.herokuapp.com/
System information
Some dependencies:
"webpack": "^4.29.6"
"next-routes": "^1.4.2",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"dotenv": "^6.2.0"
Any idea how I can fix this? I've been tring for over a week :(
The text was updated successfully, but these errors were encountered: