Next JS env variables not working in production

Viewed 1813

According to Next.js documentation, you need to declare your env variables in next.config.js under env key to read them at build time:

env: {
    GOOGLE_ANALYTICS_ID: process.env.GOOGLE_ANALYTICS_ID
  }, 

This is working correctly in my local environment, the variables are hidden from _NEXT_DATA and I can still access them from the client. The problem is when deploying the application for production, it can no longer read from process.env. Has anyone encountered this issue? Note: I don't want to expose sensitive data. Will adding the NEXT_PUBLIC_ prefix to the env variables expose them to _NEXT_DATA as well?

1 Answers

You should store your env variables in .env (for Next.js versions 9.4 and up) file like it's shown here. In case you are deploying to Vercel refer to this guide

Related