question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NEXT_PUBLIC_ prefix key is not readed in client side

See original GitHub issue

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

12.16.2

What browser are you using?

Chrome

What operating system are you using?

Ubuntu Mate

How are you deploying your application?

Google cloud Run - Secrets

Describe the Bug

I am using this example to use google analytics in my project. My variable is NEXT_PUBLIC_GA_ID When I use .env.local in local client side is reading the enviroment variable successfully. image

But when I deploy on google cloud run the environment variable is not readed in client side (undefined). In google cloud I use google secrets to give access to some env-vars in this container (I am not using .env.local there). image

I checked Google Secrets configuration in cloud run with a print in server logs image Also I am return this variable in /api/hello endpoint and I read correctly (Obvioslly is server side returning) image

image To summary: I have access to this env var in server side but not in client side even though I am using prefix NEXT_PUBLIC_

Expected Behavior

image

To Reproduce

I used exactly this code https://github.com/vercel/next.js/tree/canary/examples/with-google-analytics

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
balazsorban44commented, Jul 15, 2021

Client-side variables are inlined, meaning they will be replaced with their actual value build time! Setting it runtime doesn’t have an effect in the client bundle. Make sure the env variable is present when you build your app.

0reactions
balazsorban44commented, Jan 27, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Environment variables not working (Next.JS 9.4.4)
Add the prefix NEXT_PUBLIC_ to all of your environment variables. ... You can't make this kind of request from the client-side without ...
Read more >
Basic Features: Environment Variables - Next.js
Learn to add and access environment variables in your Next.js application.
Read more >
Environment Variables in Next.js - Frontend Digest
The environment variable is not yet defined on the client, so undefined gets logged ... Only the variables with that prefix will be...
Read more >
Protecting your API keys with Next JS - DEV Community ‍ ‍
And instead of using our key on the client side, we use it inside hello.js . This variable won't work on the client...
Read more >
Handling sensitive client-side API keys in Next - Code Gino
Using the above example will surely help us not leak the API key in our codebase; however, it is still accessible to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found