Search code examples
next.jsenvironment-variablesback4app

Environment variables in back4app node container


I have a simple next.js app that I am trying to deploy on back4app . Following the explanation here . My deployment seems to be going fine until it reaches where I read process.env.API_KEY in my code. It then fails during static files generation. I printed the environment variables returned and I confirmed that all the enviroment variables I set on back4app.com are not available inside the process.env . I have an enviroment variables like the snapshots below envronment variables . Yet none of them get loaded on back4app container. I am wondering please is there anything I am doing wrong ? Have spent hours thinking about it yet couldn't find something wrong. My simple Docker file is

FROM node:latest
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "run", "start"]

Any help will highly be appreciated.


Solution

  • The environment variables are mostly related to the building process and not to the environment variables of your project.

    You might need to set the variables in your Dockerfile to something like this:

    # Define an argument with a default value
    ARG VARNAME=init
    
    # Set an environment variable using the ARG value
    ENV VARNAME=$VARNAME