Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Next.JS environment variables on client side?

I have a Next.Js application that I will deploy with docker. I am passing my environment variables in docker file and docker-compose.yaml. Next version: 12.1.6

Dockerfile

# Install dependencies only when needed
FROM node:16-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# If using npm with a `package-lock.json` comment out above and use below instead
# COPY package.json package-lock.json ./ 
# RUN npm ci

# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN yarn build

# If using npm comment out above and use below instead
# RUN npm run build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

ENV NEXT_PUBLIC_BASE_URL example --> I'm stating it here. Example is not my value, it just takes space.


# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json

# Automatically leverage output traces to reduce image size 
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]

docker-compose.yaml

version: '3'

services:
  frontend:
    image: caneral:test-1
    ports:
      - '3000:3000'
    environment:
      - NEXT_PUBLIC_BASE_URL=https://example.com/api

I am building with the following command:

docker build -t caneral:test-1 .

Then I run docker-compose:

docker-compose up -d

While I can access the NEXT_PUBLIC_BASE_URL value on the server side, I cannot access it on the client side. It returns undefined. Shouldn't I reach it because I define it as NEXT_PUBLIC? This is stated in the official documents.

like image 200
Caner Avatar asked Dec 07 '25 05:12

Caner


2 Answers

To access the environment variables (.env) from client-side you have to prefix the variables with NEXT_PUBLIC_

For example : if your env variable is MY_PASSWORD=12345 inside.env or .env.local change it to NEXT_PUBLIC_MY_PASSWORD=12345

IMPORTANT : This will expose your environment variables to the browser and is not safe. Only do this if necessary.

like image 141
Abhith Shaji Avatar answered Dec 08 '25 20:12

Abhith Shaji


How to get environment variables on client side?

Details, you have:

  1. Your .env file. (i'm not sure how docker files will affect logic)
  2. Your next.config.js file

Server-side has access to these 2 files.

Client-side doesn't have access to .env file.

What you can do:

In your next.config.js file you can declare a variable where value is your process.env value.

const baseTrustFactor = process.env.trustFactor

IMPORTANT: do not expose your private info (keys/tokens etc.) to the client-side.

If you need to compare the tokens you can:

  1. Send them from the backend (From NodeAPI or similar)
  2. Make conditions in next.config.js such as:

const baseTrustFactor = process.env.trustFactor == '21' ? true : false

like image 41
illia chill Avatar answered Dec 08 '25 19:12

illia chill