Next.js Discord

Discord Forum

How to force Next.JS to use different env files?

Unanswered
Little Shearwater posted this in #help-forum
Open in Discord
Little ShearwaterOP
It seems that Next.JS automatically (or by default) loads env files based on the name (in order)
1. .env.local
2. .env.test
3. .env.production

The thing is I don't want to name like these since I may have multiple env files, I'd like to name it to be .env.beta or .env.example but knowing the order, if I remove these files .env.production or other, Next.JS will load NONE.

14 Replies

Little ShearwaterOP
This occurs even when I set the env file in docker compose.
services:
  beta-rblxwork:
    container_name: beta-rblxwork-container
    image: beta-rblxwork-image:latest
    build: .
    ports:
      - "1000:1000"
    env_file:
      - .env.beta
    environment:
      - PORT=1000
    restart: unless-stopped
    command: ["npm", "run", "start"]
I think this occurs during build-time, not run-time.
Rose-breasted Grosbeak
Seems like a common use case, it should be in the docs
🙏 🙏 🙏 🙏
This is the first thing that came to my mind when using NextJS for first time
Short mackerel
You can make another variable in the env called environment and call different variables based on the enviromnent conditionally without using docker
Not sure if it could cause runtime issues
I'd like to do it at build-time if possible
Rose-breasted Grosbeak
Idk try it out