Skip to content

use --env-file on localhost supabase start / stop / reset #1518

Closed

Description

@jdgamble555

Describe the bug
I'm not sure if this is a bug or a needed feature, but I would like to be able to load a different .env file name besides just .env. Apparently this is possible for local edge functions per Managing Secrets and Environment Variables, but not possible for the whole supabase? This doesn't make sense to me why it would only be available for local function testing and not the whole config.toml, so perhaps I am missing something?

To Reproduce
Steps to reproduce the behavior:
Run localhost with supabase start --env-file ./.env.development

Expected behavior
It would load the .env.development (or whatever name), instead of throwing an error:

unknown flag: --env-file

Desktop (please complete the following information):

  • OS: Windows 11
  • Version of CLI 1.99.5
  • Version of Node.js 18

Thanks,

J

Activity

  1. bombillazo commented on Sep 22, 2023

    @bombillazo

    That I know of, supabase start does not use a --env.file flag, but you can source the env file you want to use locally in your terminal and supabase start will see those variables when you run start.

  2. jdgamble555 commented on Sep 22, 2023

    @jdgamble555
    Author

    source is specific to unix and still a work around. I am using Windows (statistically most programmers do), and I don't want to declare all my projects env variables globally.

    This needs to be implemented as a --env-file flag so that it can work for Windows users as well.

    J

  3. added
    enhancement
    on Sep 23, 2023
  4. jdgamble555 commented on Sep 23, 2023

    @jdgamble555
    Author

    I should add that this works:

    supabase functions serve --env-file ./path/to/.env-file
    

    But we need this to work:

    supabase start --env-file ./path/to/.env-file
    supabase stop --env-file ./path/to/.env-file
    supabase reset --env-file ./path/to/.env-file
    ...
    

    So not just supabase start.

    J

  5. changed the title [-]use --env-file on localhost supabase start[/-] [+]use --env-file on localhost supabase start / stop / reset[/+] on Sep 23, 2023
  6. msawangwan commented on Oct 10, 2023

    @msawangwan

    this would be much appreciated.

  7. Matsuuu commented on Jan 11, 2024

    @Matsuuu

    I've also come to want this. Mainly because I want to be creating a dev setup, where I could have a simple setup steps for our dev team on varying machines (unix, windows, etc.) without having to have separate instructions on each machine.

    Running supabase from npx or a local installation is already quite cross-platform but if the project has any env variables, it becomes a burdain to make cross platform.

    After reading through Function.run codes and Start.run codebases, I don't think it's impossible to just Boop some env variables on board.

    Actually implementing it to all of the cli api's though? That might require some monkey patching which I don't know is the best approach.

    Could there be an approach where we create a loader for --env-file files and use it anywhere in the system? That would allow us to add this functionality to all of the cli endpoints, without having to trickle down the variable through all of the function calls.

    Something along the lines of (writing js as go isn't my strong suit)

    class LocalEnvManager {
      #instance;
      static getInstance() {
        if (!this.#instance) {
          this.#instance = new LocalEnvManager();
        }
        return this.#instance;
      }
    
      readSuppliedEnv(envFilePath) {
            // envFilePath is supplied by user via `--env-file`
    	const userEnv = parseEnvFile(envFilePath, fsys) // This function is already in https://github.com/supabase/cli/blob/main/internal/functions/serve/serve.go#L180
      }
    }

    And then we could just call the singleton instance throughout our cli pathways to trickle them around the different utils.DockerStart calls.


    Would this singleton-ish approach make any sense? It would allow us to utilize the same flag across the cli API without having to completely butcher the function calls by adding more and more params to them and trickling the data down all of the pipes.

    I'd be happy to take a stab at this if there's interest. Ping @sweatybridge or someone who's actively maintaining this project.

  8. kobago commented on Jan 12, 2024

    @kobago

    Personally, I would be happy if .env.local is automatically used in the local development environment.
    .env.local is often included in .gitignore and can safely handle environmental counts.

  9. sweatybridge commented on Apr 20, 2024

    @sweatybridge
    Contributor

    Personally, I would be happy if .env.local is automatically used in the local development environment.

    I like this suggestion as well. We can probably follow the convention documented by godotenv. While it's not as explicit as --env-file flag, it does save quite a lot of keystrokes when starting and stopping.

    If anyone would like to contribute a PR, this would be the code to update:

    if err := godotenv.Load(); err != nil && !errors.Is(err, os.ErrNotExist) {

  10. sweatybridge commented on Apr 30, 2024

    @sweatybridge
    Contributor

    CLI now supports the following precedence order when loading .env files

    Priority Environment .gitignoreit? Notes
    development test production
    highest .env.development.local .env.test.local .env.production.local Yes Environment-specific local overrides
    2nd .env.local N/A .env.local Yes Local overrides
    3rd .env.development .env.test .env.production No Shared environment-specific variables
    last .env .env .env Maybe Shared for all environments

    Ref: https://github.com/bkeepers/dotenv?tab=readme-ov-file#customizing-rails

  11. gorango commented on Sep 2, 2024

    @gorango

    After upgrading to the latest version of Supabase, I am still unable to use environment variables in my local env, getting:

    Error evaluating "env(<VARIABLE>)": environment variable <VARIABLE> is unset.
    

    I tried to source .env before running supabase start. And I had the same results renaming the env file .env.development.local, .env.local, and .env.development.

    I also reset all docker containers and volumes with docker rm $(docker ps -a -q) and docker volume rm $(docker volume ls -q --filter dangling=true)

    Desktop environment

    • OS: Ubuntu 22.04.4 LTS x86
    • CLI: v1.191.3 and v1.117.0
    • Node: v20.12.0

    Update

    Solved. In the .env file, prepending export before each variable and calling source .env && supabase <command> works.

    However, this is undocumented.

  12. tommedema commented on Sep 7, 2024

    @tommedema

    none of these .env files seem to work for me, including prefixing export. E.g. when I set AUTH_JWT_SECRET in an .env then restart supbase it still uses the default key:

    JWT secret: super-secret-jwt-token-with-at-least-32-characters-long

    For functions I need to use npx supabase functions serve --env-file .env but for other commands there's no such option and there's no mention in the docs on how to actually set the JWT secret either

  13. sweatybridge commented on Sep 8, 2024

    @sweatybridge
    Contributor

    You want to use SUPABASE_AUTH_JWT_SECRET in .env file. I will create a separate issue to improve the docs on config.toml since that trips up users quite often.

  14. komposer-aml commented on Sep 29, 2024

    @komposer-aml

    Hi @sweatybridge thank you for all of your work! Where can I find documentation on what the .env file key names should be? Specifically for the anon and service key definitions

  15. komposer-aml commented on Sep 29, 2024

    @komposer-aml

    Never mind I found something!! If anyone else finds this via a Google search, I was able to find it at this repo's README:
    https://github.com/supabase/fly-preview

  16. manizm commented on Oct 15, 2024

    @manizm

    Is this supposed to substitute values in config.toml as well when starting? i.e. project-id = "env(PROJECT_ID)" is not getting substituted by values present in .env file. It didn't work by adding values through dotenv-cli either.

    My project structure is:

    package.json
    .env
    supabase
    |-- config.toml
    |-- other files
    
  17. joelpramos commented on Dec 25, 2025

    @joelpramos

    @sweatybridge this didn't work for me for functions with the supabase start with any combination of SUPABASE_ENV=local supabase start or sourcing before as suggested above source supabase/functions/.env.local && supabase start... etc. Tried the files at different levels (repo root, supabase/, supabase/functions).

    only the variables under .env on supabase/functions are pulled into the Edge functions runtime.

    any thoughts here?

  18. jdgamble555 commented on Dec 25, 2025

    @jdgamble555
    Author

    For me, I use .env for local testing, and if I need to test prod locally, it won't be a local supabase (you can only run one at a time anyway), it will connect directly to my hosted prod database. If I had a staging, it would probably be a hosted free tier for testing only.

    So not really needed when you think about it.

    J

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions