.env.local.production File

GitHub Actions or GitLab CI often run next build in a production environment but need a build-time secret that differs from runtime.

# .github/workflows/deploy.yml
- name: Create .env.production.local
  run: |
    echo "BUILD_CACHE_TOKEN=$ secrets.CI_TOKEN " > .env.production.local
    npm run build

Create a file pages/api/debug.js:

export default function handler(req, res) 
  res.status(200).json(
    nodeEnv: process.env.NODE_ENV,
    customVar: process.env.MY_CUSTOM_VAR,
    // Warning: Do not do this in real production
    allEnv: process.env
  );

Visit /api/debug and look for your variable.

The Power of .env.local.production: Managing Environment-Specific Variables in Production

As your application grows in complexity, managing environment-specific variables becomes increasingly important. In production environments, it's crucial to keep sensitive information, such as API keys and database credentials, secure and separate from your codebase. One effective way to achieve this is by using a .env.local.production file. In this article, we'll explore the benefits and best practices of using .env.local.production to manage environment-specific variables in production. .env.local.production

What is .env.local.production?

.env.local.production is a file that stores environment-specific variables for a production environment. It's a variation of the popular .env file, which is used to store environment variables for local development. The .local and .production suffixes indicate that this file is specific to the local production environment.

Benefits of using .env.local.production

Best practices for using .env.local.production GitHub Actions or GitLab CI often run next

Example use case

Suppose you're building a web application that uses a third-party API to authenticate users. You have a production environment set up on a cloud platform, and you want to keep your API key secure. You can create a .env.local.production file with the following content:

API_KEY=your_production_api_key_here
API_SECRET=your_production_api_secret_here

In your application code, you can then reference these variables using a library like dotenv:

require('dotenv').config(
  path: `.env.local.$process.env.NODE_ENV`,
);
const apiKey = process.env.API_KEY;
const apiSecret = process.env.API_SECRET;

Conclusion

.env.local.production is a powerful tool for managing environment-specific variables in production environments. By keeping sensitive information separate from your codebase and following best practices, you can ensure a secure and flexible deployment process. Whether you're building a small web application or a large-scale enterprise system, .env.local.production is an essential file to have in your toolkit.


Because .env.local.production is gitignored by default (if you follow standard patterns like *.local), it avoids accidental exposure. However:

🔐 Best practice: Use .env.production.local only for non-sensitive overrides or during local debugging. For real production secrets, use cloud secret stores or CI/CD environment variables.

Next.js 9.4+ introduced built-in support for dotenv expansion. Create a file pages/api/debug

Example:

# .env.production
API_URL=https://api.myapp.com