Next.js Discord

Discord Forum

How do you use .env and .env.local variables?

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
I have been following the official [NextJS](https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#loading-environment-variables) page to understand how to use .env.local, but my variables don't work unless I use them on .env.. What am I missing?

26 Replies

Plott Hound
Are the env variables related to prisma by any chance? I had trouble with it using anything but .env
@Plott Hound Are the env variables related to prisma by any chance? I had trouble with it using anything but .env
Transvaal lionOP
No prisma. Just NextJS and I had .env with .env.local, but then removed .env and it still doesn't work.
Plott Hound
Did you restart the server after making the changes? They only load when you run
@Plott Hound Did you restart the server after making the changes? They only load when you run
Transvaal lionOP
I have, but I will redo it.
Plott Hound
If that doesn’t do it make sure you only have one .env file loaded. Next will load them in this order until it finds one:

Environment Variable Load Order

Environment variables are looked up in the following places, in order, stopping once the variable is found.

process.env
.env.$(NODE_ENV).local
.env.local (Not checked when NODE_ENV is test.)
.env.$(NODE_ENV)
.env
I assume you are working in a local dev environment but if you’re doing it on a deployment on eg vercel you’ll need to rebuild whenever you make any env changes
Transvaal lionOP
Do these environment variables are seen by the compiler when you run pnpm run dev?
Transvaal lionOP
Not sure why they make it so confusing in their docs when they use a .env.local in their official documentation and later state that the same file will not be checked.
Plott Hound
If you’re using pnpm run dev then NODE_ENV would be development. So it will still be loaded
@Plott Hound If you’re using pnpm run dev then NODE_ENV would be development. So it will still be loaded
Transvaal lionOP
I switched to .env.development.
I still don't understand the use of adding .local to the end of an .env file.
@Transvaal lion I switched to `.env.development`. I still don't understand the use of adding `.local` to the end of an `.env` file.
Plott Hound
Typically you would either just use a .env for everything if it’s just a small local project. But later on it’s useful to have a .env.local for testing locally and a .env.production for production. For stuff like oauth when the env files contents will be different. If you’re just messing around locally you can use .env.local or .env
How you use them is really up to your working environment and personal preference
@Plott Hound Typically you would either just use a .env for everything if it’s just a small local project. But later on it’s useful to have a .env.local for testing locally and a .env.production for production. For stuff like oauth when the env files contents will be different. If you’re just messing around locally you can use .env.local or .env
Transvaal lionOP
If I would want to use a specific environment to test out the functionality of my application, why not just use .env.development? Isn't that the purpose of .env.development? How is it different from .env.development.local then? Is one used in my local computer and another in my cloud?
Plott Hound
You can use whatever you want. Env.development env.local env.production are for different use cases
Is your problem still with nextjs not loading .env.local? If not please mark this as solved
@Plott Hound Is your problem still with nextjs not loading .env.local? If not please mark this as solved
Transvaal lionOP
The purpose of this thread is to have a better understanding of .env and .env.local which I am still unclear and have not closed it for that reason.
@Transvaal lion If I would want to use a specific environment to test out the functionality of my application, why not just use `.env.development`? Isn't that the purpose of `.env.development`? How is it different from `.env.development.local` then? Is one used in my local computer and another in my cloud?
Aleutian Tern
normally on a project we have a lot of env for a lot of env/setup. for example, your team could have the .env.development variables, but your task needs to work on theses env to test it. instead of overriding the .env.development, you create the .local that have more priority on the load without touching the .env. Is convenient to have a built-in way to apply priority on envs variables.
Aleutian Tern
Kind, you can also use whatever you prefer, is more about being convenient to have built-in ways to "override" env variables if need.
@Aleutian Tern Kind, you can also use whatever you prefer, is more about being convenient to have built-in ways to "override" env variables if need.
Transvaal lionOP
I think it would make sense that .env.{environment} files would be meant for core variables and the .local would be for any additional things. I understand that it depends on what me as a developer would prefer, but I want to have at least an idea on where to start defining it and the previous seems like a good definition.