#BetterAuth CLI not recognizing env vars

4 messages · Page 1 of 1 (latest)

potent grove
#

Why is BetterAuth CLI not getting the env vars available in .env.local? ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: Missing environment variable EXPO_PUBLIC_MOBILE_URLI am trying to implement local install (https://convex-better-auth.netlify.app/local-install) but fails when running cd convex/betterAuth
npx @better-auth/cli generate -y`

rain lilyBOT
#

Hi @potent grove. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The Better Auth CLI does not automatically load environment variables from your .env.local file. This is a common source of confusion. When you run commands like npx @better-auth/cli generate -y, the CLI only has access to environment variables that are present in the current shell environment—not those defined in .env.local or other env files unless you explicitly load them.

To resolve this, you have a couple of options:

  1. Export the variable in your shell before running the CLI command:

    export EXPO_PUBLIC_MOBILE_URL=your_value_here
    npx @better-auth/cli generate -y
    
  2. Use a tool like dotenv-cli to load variables from your .env.local file:

    npx dotenv -e ../.env.local -- npx @better-auth/cli generate -y
    

    (Make sure dotenv-cli is installed in your project.)

This behavior is not unique to Better Auth; most CLIs do not automatically parse .env.local files unless specifically designed to do so. The knowledge sources confirm that environment variables in .env.local are not automatically available to CLI tools, and you need to ensure they are present in the shell environment when running such commands. This is also why, for Convex deployments, environment variables must be set via the dashboard or the npx convex env set command, not just in .env.local files, as those files are primarily for frontend frameworks and the Convex CLI, not for backend code or CLI tools running in isolation Local Install Discord: R2 component shows missing env variables despite being set.

#

If you continue to see errors about missing environment variables, double-check that the variable is exported in your shell or loaded with a tool like dotenv-cli before running the Better Auth CLI.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: