Can't get updated .env to load from built Next.js when running npm start #29199
Replies: 6 comments 3 replies
-
Once the app is built, any While any One could argue that variables placed within Encryption aside, ideally, anything sensitive should be accessible only to the server; while anything accessible to the client should be considered public domain. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply @mattcarlotta- in my case all of the environmental variables in question are 100% public, I very much want them to be exposed to the client but I can't hardcode them in because I run my app on different installations and want to switch out the site name and available modes for each site while sharing the same docker image for all of them. When I try Thank you! |
Beta Was this translation helpful? Give feedback.
-
@CaptainChemist Apologies for the late response. There's an .env.production APP_TEST=567 .env APP_TEST=123 Then If you need an ENV to be dynamic, then you'll have to use .env.production APP_TEST=567 pages/index.js import getConfig from "next/config";
const { publicRuntimeConfig } = getConfig();
// export default function Home({ APP_TEST }) {
export default function Home() {
return (
<div className="container">
<main>
<h1 className="description">
random var test: {publicRuntimeConfig.APP_TEST}
{/* random var test: {APP_TEST} */}
</h1>
<h1 className="description">
next_public var test: {process.env.NEXT_PUBLIC_TEST}
</h1>
</main>
</div>
);
}
// optionally, you can pass the ENV back from getServerSideProps
// this way you won't need to use publicRuntimeConfig variables
export const getServerSideProps = () => ({
props: {},
// props: { APP_TEST: process.env.APP_TEST },
}); Run Update .env.production: APP_TEST=890 Run The reason WHY it works is because the |
Beta Was this translation helpful? Give feedback.
-
how can i add third or fourth value? In addition to development (.env.development) or production (.env.production), I also use staging (.env.????) |
Beta Was this translation helpful? Give feedback.
-
If you run this on the integrated IDE terminal, then the issue persists. When I run it on a separate terminal outside of the IDE, it tends to use the env variables I have given. this is the easiest way to solve ur problem |
Beta Was this translation helpful? Give feedback.
-
I had the same issue. It seems like I have exported the .env at some point, probably happy trigger fingers. So when I do: printenv | grep NEXT_ I see: NEXT_PUBLIC_BASE_URL=https://SOME_OLD_VALUE.vercel.app So I had to: unset NEXT_PUBLIC_BASE_URL And now the .env file is doing what it's suppose to. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What version of Next.js are you using?
11.0.0
What version of Node.js are you using?
12.13.0
What browser are you using?
Chrome
What operating system are you using?
Mac OS 11.2.3
How are you deploying your application?
next start
Describe the Bug
When I run
npm start
, I see in the terminal that my env is being loaded, but despite that message, in the browser, I see environmental variable definitions that were present during the build step, not the current run step.This surprises me that it happens at all and what surprises me, even more, is that we see this both for a NEXT_PUBLIC variable and also a generic REACT_APP variable that I've passed through using the publicRuntimeConfig options in the
next.config.js
file.This is such a common request that I'm sure I must be doing something wrong, but it is strange that I can see this even with the starter repo as well. Thank you for taking a look!
Expected Behavior
I'd expect that with both using the NEXT_PUBLIC prefix or using the
publicRuntimeConfig
variable definition that I should see the updated variable definitions come through for the run start command, not the leftover definitions that were present during the build.To Reproduce
Reproduction repo:
https://github.com/CaptainChemist/nextjs-env-learn-starter
Steps to reproduce:
.env.build
to.env
and.env.local
yarn
thenyarn run dev
yarn run build
.env.run
to.env
and.env.local
yarn run start
and confirm that the build variable definitions come through, not the run variable definitions.Beta Was this translation helpful? Give feedback.
All reactions