How to create environment variables only to be used in the production version of Nextjs?

February 24, 2021 - 1 min read

To create environment variables that should be used only in the production version or build of your Nextjs project, you can make a file called .env.production at the root of the Nextjs project folder.

This file will be used (along with the .env if you have it defined) to take environment variables in the production build or when using the next start command in the terminal.

For example, let's say you have an environment variable called SERVER_ID=12345 which you need to use only in the production build of your Next.js project, so we can define this environment variable in the .env.production file like this,

.env.production file

SERVER_ID = 12345;

Feel free to share if you found this useful 😃.