17

I have a NEXT_PUBLIC environment variable in my NextJS application. It has been set in my .env.local file and working correctly but after it has been deployed to Azure App Service the app is unable to read it. It is 'undefined'. I have configured it under Configuration for the App Service.enter image description here

Any advice on what the issue might be?

Thanks in advance.

5
  • Did you find a solution? because I have the same problem atm. Commented Apr 13, 2022 at 12:22
  • 1
    @Max I was unable to resolve the PUBLIC issue but to resolve the issue I was facing was to make use of getServerSideProps. Then I don't need the PUBLIC environment variable. Hope you find a solution. If you do please add it for everyone else to see. Commented Apr 14, 2022 at 8:44
  • @Max did you find a solution by any chance? Having a similar issue. Commented Nov 7, 2022 at 19:07
  • @Gericke sorry I didn't find a solution. Commented Nov 8, 2022 at 20:31
  • Any updates on the solution? Facing similar issue. Commented Dec 9, 2022 at 11:03

3 Answers 3

9

I had the same problem that the NEXT_PUBLIC environment variables on my Azure AppService always had the value "undefined", although they were defined as environment variables in the AppService settings.

When building the Docker image, the NEXT_PUBLIC environment variables are set directly during the build and cannot be overwritten afterwards. This was the reason why the settings from the AppService for the NEXT_PUBLIC did not work.

The following solution by Renato Pozzi (dev.to) helped me.

Here are my steps: (next v.12.2.5):

a) Create the desired NEXT_PUBLIC variable in the Dockerfile before the "build" process start and fill it with a "placeholder value".

RUN NEXT_PUBLIC_API_URL=PLACEHOLDER_NEXT_PUBLIC_API_URL yarn build

b) Create an entrypoint file (entrypoint.sh) which replaces the placeholder value with real environment variables from the AppService when the Docker image start.

In my example, the placeholder "PLACEHOLDER_NEXT_PUBLIC_API_URL" is replaced with the real value "NEXT_PUBLIC_API_URL" from the AppService configurations (environment variables).

#!/bin/sh
test -n "$NEXT_PUBLIC_API_URL"
find /app/.next \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_NEXT_PUBLIC_API_URL#$NEXT_PUBLIC_API_URL#g"
exec "$@"

c) Make the previously created entry point file known in the Dockerfile.

COPY --chown=nextjs:nodejs entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["node", "server.js"]
Sign up to request clarification or add additional context in comments.

Comments

0

I also ran into this issue, and Paul is right. You need to set the variables during build time. I'm running a CICD github action for deployment and this solved it:

      - name: Create .env.local file
        run: echo "NEXT_PUBLIC_MY_KEY=${{ secrets.NEXT_PUBLIC_MY_KEY }}" > .env.local

From the nextjs docs, environment variables in .env.local will take precedence:

https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#default-environment-variables

Comments

0

If you are using the Github actions to deploy the image

  - name: Create .env file
        uses: SpicyPizza/[email protected]
        with:
          envkey_NEXT_PUBLIC_GOOGLE_API_KEY: ${{ env.NEXT_PUBLIC_GOOGLE_API_KEY }}
          envkey_NEXT_PUBLIC_BACKEND_URL: ${{ env.NEXT_PUBLIC_BACKEND_URL }}         
          file_name: .env

      
      - name: Build and Push to ACR
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: ${{ env.ACR_LOGIN_SERVER }}/test:latest
          file: Dockerfile
          context: .

context: . is required; otherwise, the .env file won't be copied into the docker image.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.