#.env Variables undefined

1 messages · Page 1 of 1 (latest)

floral pewter
#

Hi, im trying to set up betterauth but when I run
npx @better-auth/cli generate

I get the following error:

2025-08-05T12:05:44.299Z WARN [Better Auth]: Social provider discord is missing clientId or clientSecret
- preparing schema...node:internal/process/promises:394
    triggerUncaughtException(err, true /* fromPromise */);
    ^

AggregateError [ECONNREFUSED]:
    at internalConnectMultiple (node:net:1134:18)
    at afterConnectMultiple (node:net:1715:7) {
  code: 'ECONNREFUSED',
  fatal: true,
  [errors]: [
    Error: connect ECONNREFUSED ::1:3306
        at createConnectionError (node:net:1678:14)
        at afterConnectMultiple (node:net:1708:16) {
      errno: -4078,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '::1',
      port: 3306
    },
    Error: connect ECONNREFUSED 127.0.0.1:3306
        at createConnectionError (node:net:1678:14)
        at afterConnectMultiple (node:net:1708:16) {
      errno: -4078,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '127.0.0.1',
      port: 3306
    }
  ]
}

Node.js v24.5.0

Now I believe I have narrowed it down to the environment variables being undefined so I wanted to make sure im not doing anything wrong?

I've created a .env.local file in the root of the nextjs project

<project-name>
-/src
-- /app
- .env.local

Then in my
/<project-name>/lib/auth.ts
I reference the variables in the following way

dusky compassBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

floral pewter
#
import { betterAuth } from "better-auth";
import { createPool } from "mysql2/promise";

export const auth = betterAuth({
    database: createPool({
        host: process.env.MYSQL_HOST || "localhost",
        user: process.env.MYSQL_USER || "root",
        password: process.env.MYSQL_PASSWORD || "",
        database: process.env.MYSQL_DATABASE || "railway",
        port: process.env.MYSQL_PORT ? Number(process.env.MYSQL_PORT) : 3306,
        waitForConnections: true,
        connectionLimit: 10,
        queueLimit: 0,
    }),
    emailAndPassword: {
        enabled: false,
    },
    socialProviders: {
        discord: {
            clientId: process.env.DISCORD_CLIENT_ID as string,
            clientSecret: process.env.DISCORD_CLIENT_SECRET as string,
        },
    },
});

Am I doing something wrong? are there any additional steps I need to take in order for the process.env.<variablename> to work?

#

I also made a server action in
/lib/actions.ts

which is called in a route

/app/api/debug/env/route.ts

"use server"

// Utility function to log environment variables for debugging (server-side only)
export async function logEnvironmentVariables() {
    // Double-check we're on server side
    if (typeof window !== 'undefined') {
        console.log("Environment variables logging skipped - running on client side");
        return;
    }
    
    console.log("=== Environment Variables Debug (Server-Side) ===");
    const envVars = {
        MYSQL_HOST: process.env.MYSQL_HOST,
        MYSQL_USER: process.env.MYSQL_USER,
        MYSQL_PASSWORD: process.env.MYSQL_PASSWORD,
        MYSQL_DATABASE: process.env.MYSQL_DATABASE,
        MYSQL_PORT: process.env.MYSQL_PORT,
        DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
        DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
        NODE_ENV: process.env.NODE_ENV,
    };
    
    Object.entries(envVars).forEach(([key, value]) => {
        console.log(`${key}: ${value || "[UNDEFINED]"}`);
    });
    console.log("================================================");
};

and i get the following result:

=== Environment Variables Debug (Server-Side) ===
MYSQL_HOST: [UNDEFINED]
MYSQL_USER: [UNDEFINED]
MYSQL_PASSWORD: [UNDEFINED]
MYSQL_DATABASE: [UNDEFINED]
MYSQL_PORT: [UNDEFINED]
DISCORD_CLIENT_ID: [UNDEFINED]
DISCORD_CLIENT_SECRET: [UNDEFINED]
NODE_ENV: development
================================================
urban mural
#

looking at their docs, Im guessing the betterauth cli when it runs is only looking for .env and not .env.local

#

so you could try renaming .env.local temporarily, just while you run npx @better-auth/cli generate

#

or you could run it via something that injects the env vars, or you could just pass in the required vars via the command when you run it

#

BETTER_AUTH_SECRET=xxx BETTER_AUTH_URL=yyy npx @better-auth/cli generate

#

hm digging a bit more and it looks like better auth is at least finding your /lib/auth.ts file

#

and then run the command as varlock run -- npx @better-auth/cli generate

floral pewter
#

which runs while nextjs is running

#

everything is undefined

#

and even renaming to .env

#

doesnt work

urban mural
#

what version of next?

floral pewter
#

15.4.5

urban mural
#

whats in your .env.development file

#

also, what happens if you get rid of --turbopack

floral pewter
#
MYSQL_HOST=stringwithdots
MYSQL_USER=astring
MYSQL_PASSWORD=string
MYSQL_PORT=number
MYSQL_DATABASE=string

DISCORD_CLIENT_ID=number
DISCORD_CLIENT_SECRET=stringwithcharacters.-
BETTER_AUTH_SECRET=stringandnumbers
BETTER_AUTH_URL=http://localhost:3000
#

with no quotes

#

same thing

 ✓ Compiled /_not-found in 2.2s (718 modules)
=== Environment Variables Debug (Server-Side) ===
MYSQL_HOST: [UNDEFINED]
MYSQL_USER: [UNDEFINED]
MYSQL_PASSWORD: [UNDEFINED]
MYSQL_DATABASE: [UNDEFINED]
MYSQL_PORT: [UNDEFINED]
DISCORD_CLIENT_ID: [UNDEFINED]
DISCORD_CLIENT_SECRET: [UNDEFINED]
NODE_ENV: development
#

I just tried a NEXT_PUBLIC_ variable too

#

and didnt work

#

im stumped

#

ive never had this issue

urban mural
#

ok so I just did fresh install

#

and added 3 env files - .env, .env.development, .env.local

#

when I run my dev command, I see all 3 listed

#

since they should all be getting loaded

floral pewter
#

ok ive just done that

#

and it worked lol

#

my syntax highlighting is only highlighter .env

#

not .env.local .env.development

#

maybe somethjing is wrong with those files

urban mural
#

you can just manually set the file association

#

its because most file types use a suffix

#

but .env is weird

floral pewter
#

ok .env.development by itself doesnt work

#

ok

#

there must be something wrong in the file
because when I copy paste the .env.development and change the names to .env and .env.local it didnt work
but when I made new files and copy pasted the content it works xD
Must've been some invisible char in the file or something

#

what the frig

urban mural
#

maybe some invisible chars or something

floral pewter
#

ye

#

something like that

urban mural
#

plus validations, and leak detection

floral pewter
#

ok and schema generation worked nice

#

mustve been that

urban mural
#

(^ I am the creator)

floral pewter
#

yes I saw xD

#

ill take a look but its just a small project so prob wont use it atleast now

#

cool website tho

urban mural
#

even so, its super easy to set up, and simple to use 🙂

#

anyway, glad it's working!

floral pewter
#

alright thanks for the help

urban mural
#

it was likely weird invisible chars

#

I think in my editor, they get highlighted and displayed. dig around in settings to see if you can find something like that