Next.js Discord

Discord Forum

.env Variables undefined

Answered
Wool sower gall maker posted this in #help-forum
Open in Discord
Wool sower gall makerOP
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
Answered by Wool sower gall maker
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
View full answer

58 Replies

Wool sower gall makerOP
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?
Wool sower gall makerOP
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
================================================
Burmese
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
you could try setting up https://varlock.dev/
and then run the command as varlock run -- npx @better-auth/cli generate
@Burmese hm digging a bit more and it looks like better auth is at least finding your /lib/auth.ts file
Wool sower gall makerOP
yes the thing is even in a server action
which runs while nextjs is running
everything is undefined
and even renaming to .env
doesnt work
Burmese
what version of next?
Wool sower gall makerOP
15.4.5
lumen-website@0.1.0 dev
next dev --turbopack

▲ Next.js 15.4.5 (Turbopack)
- Local: http://localhost:3000
- Network: http://10.14.0.2:3000
- Environments: .env.development

✓ Starting...
✓ Ready in 866ms
Burmese
whats in your .env.development file
also, what happens if you get rid of --turbopack
Wool sower gall makerOP
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
Burmese
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
Wool sower gall makerOP
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
Burmese
you can just manually set the file association
its because most file types use a suffix
but .env is weird
Wool sower gall makerOP
ok .env.development by itself doesnt work
ok
Wool sower gall makerOP
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
Answer
Wool sower gall makerOP
what the frig
Burmese
maybe some invisible chars or something
Wool sower gall makerOP
ye
something like that
Burmese
also, try out https://varlock.dev/integrations/nextjs/
it will give you more control over things
plus validations, and leak detection
Wool sower gall makerOP
ok and schema generation worked nice
mustve been that
Burmese
(^ I am the creator)
Wool sower gall makerOP
yes I saw xD
ill take a look but its just a small project so prob wont use it atleast now
cool website tho
Burmese
even so, its super easy to set up, and simple to use 🙂
anyway, glad it's working!
Wool sower gall makerOP
alright thanks for the help
Burmese
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