-
Notifications
You must be signed in to change notification settings - Fork 27.9k
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
10.0.8: Env variables redefined within next.config.js are undefined in dev mode #22811
Comments
same issue here. Saw after upgrading from next All of the console.log('next.config process.env: API_HOST', process.env.API_HOST); // this prints the API_HOST as undefined when starting the server
module.exports = {
target: 'server',
serverRuntimeConfig: {
secretKey: process.env.SECRET_KEY,
},
publicRuntimeConfig: {
apiHost: process.env.API_HOST,
googleClientId: process.env.GOOGLE_CLIENT_ID,
webHost: process.env.WEB_HOST,
},
}; |
In 10.0.0.8 development phase, in browser, process.env is now = {}. |
I solved this by prefixing all env variables that I need on the browser with NEXT_PUBLIC_ |
Yes, I am having a similar issue when I am using something like this in the
Looks like the
while in 10.0.8:
|
@violabg Using Can you give a working example of what you've done? Thanks I still have the issue on |
actually, I still have problems #22843 . |
I've been accessing values in // next.config.js
module.exports = {
images: {
domains: [
hostname(process.env.NEXT_PUBLIC_ASSETS_BASE_URL)
]
}
} This used to work before the upgrade to |
Try this version yarn add next@10.0.8-canary.9 10.0.8-canary.9+ bigger than canary 9, all of the env files are delayed loaded. |
@jsonchou thank you, your recommended version fixed all of my problems. Dynamic next/images domains, next-secure-headers, process.env variables, etc. |
This ensures we load all env values before loading `next.config.js` since these values can be used in there. This also updates to ensure we're testing these values are available while loading `next.config.js` so we don't regress on this. Fixes: #22811
Hi, this should be corrected in |
@ijjk works great now, thanks! |
@ijjk |
@OmgDef can you provide a reproduction for the issue with your custom server that was working on a previous version but not the latest? |
@ijjk I created a repo here https://github.com/OmgDef/next-env-server. By default, next version is 10.0.7. If you upgrade next, PORT and SECRET are undefined |
@OmgDef looks like you were relying on un-documented env loading behavior in your custom-server. We don't guarantee the env will be loaded immediately there and if you need this behavior you can load it manually using the |
FWIW, I can confirm that it is working for me after upgrading to |
@nfantone I tried but it doesn't work for me, even just passing a custom variable with a random value in the env property of next.config it doesn't work |
@underfisk Mind sharing your setup so we could take a look at what might need changing? |
@nfantone Yes, i did already on other issue but here is the repo (a simple version of it): https://github.com/underfisk/vercel-issue-app |
@underfisk I'm sorry - didn't know you had share it before. Completely missed that. I took a peek and the setup looked a bit foreign to me. I could not find a Could you describe the issue you're seeing some more, please? |
Oh i'm sorry, i probably did forgot to add in there because it was a sample for Vercel team but sure, here it is the next config file
Even domains are ignored |
@underfisk Where are those env vars defined? I could only fish out the Additionally, your test project is still depending on |
@nfantone No need, that's the thing, if you pass on the env object any custom, lets say PUSHER_CLIENT_KEY is directly a string in there, if you do that and try to console log on the app, its empty (the whole env object) |
How exactly are you passing the vars onto the script? And which OS are you on? The following is working just fine for me: // next.config.js
console.log('my test env var ->', process.env.TEST_ENV_VAR);
module.exports = { ... }; // package.json
{
"scripts": {
"start": "NEXT_TELEMETRY_DISABLED=1 TEST_ENV_VAR=foo next start -p 8090 -H 0.0.0.0",
}
} ❯ yarn start
yarn run v1.22.10
$ NEXT_TELEMETRY_DISABLED=1 TEST_ENV_VAR=foo next start -p 8090 -H 0.0.0.0
ready - started server on 0.0.0.0:8090, url: http://localhost:8090
info - Loaded env from /path/to/project/.env.local
my test env var -> foo |
I'm on mac big sur (latest version) |
@underfisk Yeah - yours seems to be an entirely different issue. I have no experience with From the
So sounds to me that something like |
@nfantone The funny thing is that it always worked, only when i upgraded to latest next version it stopped working, i know i need the NX custom var locally but PUSHER client and the rest is getting injected in vercel at settings so its not a problem, in our real app we have a fallback when its undefined from the env, the issue here is not that is that if you pass the custom NX_CUSTOM_VAR its not passed to the next somehow but i'm afraid there is a bigger fish which is images/domain are not being considered, what about that? If you set a valid domain of one of your CDN its not doing anything even with localhost. |
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. |
What version of Next.js are you using?
10.0.8
What version of Node.js are you using?
14.15.1
What browser are you using?
Tested in Firefox and Safari
What operating system are you using?
macOS Big Sur 11.2.1
How are you deploying your application?
yarn dev, yarn build && yarn start
Describe the Bug
Environment variables defined in
next.config.js
return undefined in page components when on dev mode (yarn dev).But running the same project on production (yarn build && yarn start) the env variables work as expected.
Expected Behavior
If env variables are set in an
.env
file at the root of the project and then exposed to Next.js within thenext.config.js
file I would expect them to be accessible in component code in both production and development environments.To Reproduce
As a control:
The text was updated successfully, but these errors were encountered: