How to create environment variables only to be used in the production version of Nextjs?
Published February 24, 2021
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,
SERVER_ID = 12345;
- Useful in decoupling information from
- Check out How to create and use environment variables in Nextjs? to get a detailed explanation on creating and using environment variables in general in Nextjs.