question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.env taking priority over .env.local

See original GitHub issue

Describe the bug

I have an API with a VITE_API_URL environment variable that I use to change where the client is pointing depending whether it’s local or in production.

I have the variables declared in .env and an override in .env.local as follows.

.env

VITE_API_URL=https://example.com/graphql

.env.local

VITE_API_URL=http://localhost:8888/graphql

I then integrate the environment variable with the following and the variable works.

const client = new ApolloClient({
  uri: import.meta.env.VITE_API_URL,
  // rest of config...
})

When developing locally with the Vite dev server, the client uses the value from .env instead of .env.local.

I was under the impression that .env values would act as defaults and that .env.local would override any variables with the same name.

Reproduction

n/a

System Info

System:
    OS: Linux 4.19 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (12) x64 Intel(R) Core(TM) i7-6850K CPU @ 3.60GHz
    Memory: 24.78 GB / 50.14 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.7.0 - ~/.nvm/versions/node/v16.7.0/bin/node
    Yarn: 1.16.0 - /mnt/c/Users/Ian/AppData/Roaming/npm/yarn
    npm: 7.20.3 - ~/.nvm/versions/node/v16.7.0/bin/npm
    Watchman: 2021.06.07.00 - /home/linuxbrew/.linuxbrew/bin/watchman
  npmPackages:
    @vitejs/plugin-vue: ^2.0.0 => 2.0.1 
    vite: ^2.8.1 => 2.8.1

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
poyohocommented, Feb 12, 2022

sorry, I mean your expect is right 😂

0reactions
Soviutcommented, Feb 12, 2022

@poyoho I just figured it out. That documentation you quoted is talking about system environment variables not being replaced by .env variables and that jogged my mind.

Well I’m using Netlify and need to run netlify dev to load functions. Netlify runs npm run dev and also loads the .env so this becomes a “system” environment variable in the netlify environment. When I just run npm run dev on its own, the .env.local gets priority.

So now I either stick with my solution where I put local values into .env and build values in .env.production, or I need to figure out how to tell Netlify not to load the .env file so it won’t turn them into system values.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Env Variables and Modes - Vite
Vite exposes env variables on the special import.meta.env object. ... (e.g. .env.production ) will take higher priority than a generic one (e.g. .env...
Read more >
What is the difference between .env.local and ... - Stack Overflow
Here's the priority of the files for the development build and the production build: Dev.: ( npm start ): .env.development.local ...
Read more >
Basic Features: Environment Variables - Next.js
Use .env.local to load environment variables; Expose environment ... them into a .env.local for usage on your local machine using the following command:...
Read more >
Create-react-app environments - Medium
Variables from the shell take precedence those in a .env file. ... Place environment specific values/overrides in .env.local file in the build location...
Read more >
We need to talk about the .env | Platform.sh
Depending on how the file is written, it may be possible to modify it manually ... Even if you're using a containerized environment...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found