Process.env values not returning in time for initAuth0()

I’ve got a wildcard subdomain set up that requires a dynamic baseURL value, so I’m initializing my own Auth0Server instance via initAuth0. The problem I’m facing, is that I’m getting a “TypeError: “secret” is required” - but only if I’m trying to pull my param values from process.env.xxxxx. If I copy and paste those exact values, and assign to params instead of using the env it works fine.

Is there some sort of delay in pulling form the ENV that it’s not getting what’s needed in time for the initAuth0 function? When I console log, the two objects are identical. If I swap to my stringParams it works fine, if I pass envParams it gives me the error.

@auth0/nextjs-auth0: "^2.2.1"
next: "13.1.6"
// works
const stringParams = {
  secret: "xxxxxxxxxxxxxxx,
  issuerBaseURL: "xxxxxxxxxxxxxxx",
  clientID: "xxxxxxxxxxxxxxx",

// error
const envParams = {
  secret: process.env.AUTH0_SECRET,
  issuerBaseURL: process.env.AUTH0_ISSUER_BASE_URL,
  clientID: process.env.AUTH0_CLIENT_ID,
  clientSecret: process.env.AUTH0_CLIENT_SECRET,

// logs identical objects
console.log({stringParams, envParams})

// but still errors when I run my app
export default initAuth0(envParams)

if I conditionally export, it works as well, But I then have to null check anytime I want to use it)


export default envParams.secret ? initAuth0(envParams) : null;
export const getServerSideProps = auth0?.withPageAuthRequired();

This is stumping the whole office, any sort of help would be appreciated. I am new to Next, but not react. Maybe I’m missing something with how envs are processed?

another workaround I have found…

const envParams = {
  secret: `${process.env.AUTH0_SECRET}`,
  issuerBaseURL: `${
    process.env.AUTH0_ISSUER_BASE_URL || ""
  clientID: `${process.env.AUTH0_CLIENT_ID}`,
  clientSecret: `${process.env.AUTH0_CLIENT_SECRET}`,

Note: if you don’t provide the or on the issuerBaseURL it throws an error saying that it needs to be a valid URI. Which, reenforces my feeling that a validation fn in the package is running too early.