-
I have a couple NEXT_PUBLIC_ variables in my .env file, but they are showing up as undefined. The normal env variables seem to be working fine. |
Beta Was this translation helpful? Give feedback.
Replies: 16 comments 31 replies
-
Any chance you can share some code? 😄 |
Beta Was this translation helpful? Give feedback.
-
Here's an example I put together to test: .env
pages/test.js export default () => {
console.log('variable:', process.env.NEXT_PUBLIC_TEST)
return <div>variable: {process.env.NEXT_PUBLIC_TEST}</div>
} next console output
browser console output
browser output
|
Beta Was this translation helpful? Give feedback.
-
I figured it out... I still had next console output
|
Beta Was this translation helpful? Give feedback.
-
The latest Next.js Stripe example uses |
Beta Was this translation helpful? Give feedback.
-
Unfortunately I can't remove |
Beta Was this translation helpful? Give feedback.
-
I have a monorepo with dotenv as one of its many many dependencies. I cant just uninstall dotenv. |
Beta Was this translation helpful? Give feedback.
-
If this helps anyone, I had a similar issue where my custom server was loading classes that would depend on environment variables. By initializing my next app ( So basically I'm using Next.js's implementation for all my environment variables on the server (as well as the client of course). |
Beta Was this translation helpful? Give feedback.
-
Starting to regret switching from CRA to Next. I gave up.
I fell back to As a work around, in next.config.js, I'm defining and calling ... const dotenv = require('dotenv');
function updateVariablesJson() {
// whatever is in .env.local, .env, etc ... JSONify it into > src/variables.json
const result = dotenv.config();
if (result.error) throw result.error;
fs.writeFileSync('src/variables.json', JSON.stringify(result.parsed));
} And then in
|
Beta Was this translation helpful? Give feedback.
-
create next.config.js file in root and write below code inside that:
stop server and run again #yarn dev and use in client page:
|
Beta Was this translation helpful? Give feedback.
-
You can use .env variables in next.config.js. I had the same problem using NEXT_PUBLIC_ with Next v12.0.10 and this solution worked for me: .env
next.config.js
|
Beta Was this translation helpful? Give feedback.
-
If you're using
|
Beta Was this translation helpful? Give feedback.
-
Te pwp <3 |
Beta Was this translation helpful? Give feedback.
-
The following solution by Renato Pozzi (dev.to) helped me. |
Beta Was this translation helpful? Give feedback.
-
Why are NEXT_PUBLIC variables only available if specified at build time? That's turning something that should be configurable into something that's static. I don't want to make separate builds for separate environments - I want to make 1 build that I can deploy to different environments and pass in the configuration (including variables that are exposed to the browser). I wish NextJS followed the principles of the "12 factor app" more closely. https://12factor.net/ |
Beta Was this translation helpful? Give feedback.
-
Why such a messy, complicated .env config? I haven't found a simple solution for this, that works as advertised in production deployments. |
Beta Was this translation helpful? Give feedback.
-
god This damn behavior took hours of my life. Why does it have to be this way? |
Beta Was this translation helpful? Give feedback.
I figured it out... I still had
dotenv
installed, which I guess was preventing next from loading the env variables. After removing dotenv withnpm r dotenv
everthing works as expected :)next console output