Next.js Discord

Discord Forum

I am facing issue that my .env file is not being loaded in production environment.

Answered
Russian Blue posted this in #help-forum
Open in Discord
Original message was deleted.
Answered by Russian Blue
hey man @joulev sorry for bothering i accidentaly deleted original post.. i was able to fix the issue it was something else not the ENV itself... the way i accessed the env.

i was doign process.env[key] to dynamically load many
View full answer

11 Replies

Original message was deleted
# Remove .env.local after build (not needed in runtime)
RUN rm -f .env.local


well, environment variables are needed in runtime. you need to ensure that either an .env* file exists in runtime, or that runtime otherwise already has all environment variables configured
Russian Blue
@joulev let me give you bigger picture

project
-> dashboard/ nextjs project
-> backend/ some other API project
-> .env.staging
-> .env
-> dockerfiles/dockerfile.dashboard
-> dockerfiles/dockerfile.backend
-> docker-compose.yml

dockerfile.dashboard contains what i sent first

.env has everything even NEXTPUBLIC** variables

here is docker-compose.yml

  dashboard:
    build:
      context: .
      dockerfile: ./dockerfiles/Dockerfile.dashboard
    container_name: dashboard
    platform: linux/amd64
    ports:
      - "10111:3000"
    # Load all environment variables from .env file
    env_file:
      - .env.staging # or .env 
    environment:
      - NODE_ENV=production
      - NEXTAUTH_URL=${NEXTAUTH_URL_DASHBOARD}
      - NEXTAUTH_SECRET=${NEXTAUTH_SECRET_DASHBOARD}

    depends_on:
      - qck-api-staging
    
    networks:
      - qck-network-staging
    
    restart: unless-stopped
Russian Blue
i have my .env file correctly working in backend. its similar setup
@joulev i even copied my .env to dashboard/.env.local before running docker-compose it did not work
@cp .env.staging dashboard/.env.local
docker-compose --env-file .env.staging -f docker-compose.staging.yml up -d
in makefile
Russian Blue
This issue is happening in my dev environment as well.
docker exec dashboard-dev grep "NEXT_PUBLIC" /app/.env.local
NEXT_PUBLIC_API_URL=http://localhost:10110
NEXT_PUBLIC_DASHBOARD_URL=http://localhost:10111
NEXT_PUBLIC_ADMIN_URL=http://localhost:10112
NEXT_PUBLIC_MARKETING_URL=http://localhost:10113
NEXT_PUBLIC_ENVIRONMENT=development
NEXT_PUBLIC_PASSWORD_RESET_TOKEN_MIN_LENGTH=32
NEXT_PUBLIC_PASSWORD_RESET_TOKEN_FORMAT_REGEX=^[A-Za-z0-9\-_=]+\.?[A-Za-z0-9\-_=]*\.?[A-Za-z0-9\-_=]*$
NEXT_PUBLIC_PASSWORD_RESET_SUCCESS_REDIRECT_DELAY_MS=3000
@joulev
Russian Blue
docker-composee
version: '3.8'

services:
  dashboard:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: qck-dashboard-test
    ports:
      - "4444:3000"
    volumes:
      - .:/app
      - /app/node_modules
      - /app/.next
    environment:
      - NODE_ENV=development
    command: npm run dev


dockerfile
m 
FROM node:20-alpine

WORKDIR /app

# Copy package files
COPY package.json package-lock.json* ./

# Install dependencies
RUN npm install

# Copy the rest of the application
COPY . .

EXPOSE 3000

CMD ["npm", "run", "dev"]



and .env.local is full of variables
Russian Blue
hey man @joulev sorry for bothering i accidentaly deleted original post.. i was able to fix the issue it was something else not the ENV itself... the way i accessed the env.

i was doign process.env[key] to dynamically load many
Answer