Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Env vars client side prod server

default discord avatar
eddiedev2 years ago
6

Hello, I've already tried adding

dotenv.config()

and/or


dotenv.config({path: path.resolve(__dirname, '../.env'),});

However, with the variables having

PAYLOAD_PUBLIC

in front, it still returns

undefined

.


Prod server is using

yaml

, not

env

.


What would the best solution here be? I'm suspecting

.env vars

are needed when it builds, but we can't work like that. Should I just make an

api

endpoint to server the payload public

env vars

?


Ty.

  • default discord avatar
    notchr2 years ago
    @956312590793977957

    One option is extending the exposed webpack configuration



    And using the webpack env variable plugin



    May work with YAML config?

  • default discord avatar
    eddiedev2 years ago

    hey

    @1049775120559898725

    something like this?


    // customAdminBuild.js const path = require('path'); // Tell Payload where to find your Payload config - can do it either here, or in package.json script process.env.PAYLOAD_CONFIG_PATH = path.resolve(__dirname, 'src/payload.config.ts'); const { build } = require('payload/dist/bin/build'); const buildPayloadAdmin = async () => { // Fetch your environment variables here // And then run

    build

    build(); } buildPayloadAdmin();
  • default discord avatar
    notchr2 years ago

    hmmm



    It's worth a shot! I'm not super familiar with yaml

  • default discord avatar
    eddiedev2 years ago

    unfortunately, after some research, this wont work since we cant add env variables during the build. It is added after the build. 😦

  • default discord avatar
    notchr2 years ago
    @956312590793977957

    but surely you can import the vars right?

  • default discord avatar
    eddiedev2 years ago

    hey

    @1049775120559898725

    . I couldn't apply the above since we build first and add env vars later. I Tried various things and came up with a solution. What I landed on was setting up a route to the backend to get the payload_public env var that I needed, then persisted the data using localStorage in the frontend. Not ideal, but it will do.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.