Import and Set Environment Variables from a .env file using Netlify CLI

Jason Lengstorf
InstructorJason Lengstorf
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

Environment variables set in a .env file can be imported using the Netlify CLI with the following command: netlify env:import.

This allows you to manage environment variables directly on Netlify, providing the benefit of security and flexibility of sharing environment variables with your team.

From inside Netlify, you can access the environment variables in:

Site settings > Build & deploy > Environment > Environment variables

Instructor: [0:00] As your serverless functions get more advanced, you may find yourself in a situation where you need environment variables. To add environment variable support to a serverless function, you need to use either the .env for local development, or we can add those to Netlify's environment.

[0:18] Let's start by creating a .env. I'm going to in this Hello World function. Let's just try console logging process.env.testvariable or test value. Let's call it that. What we should see when we run this, if I run netlify.dev, is that this will start up. It's going to be running at localhost:8888.

[0:46] If I head out here, we go to netlify/functions/hello-world. I run this. We see our, Hello Egghead, but in here, we see that undefined was logged.

[1:04] If I make this a little more obvious what's happening, we can say, test value, run that again, and look at our value here, and we can see that our test value is undefined.

[1:18] We want that to be set in our environment. To do that, we can start by creating a .env. In it, let's create test value, and we will say an env-var.

[1:34] Now we've set this value, if I stop and restart Netlify Dev, we can immediately see that it injected our .en file and gave us test value. If I go out here and I reload the page, I still see, Hello Egghead, but now I can see that an env-var was injected.

[1:53] That's great. However, if I wanted to share this with my team, or I wanted to put this on another computer, or I wanted to deploy this, this value currently only lives on my computer. We need to put this up on Netlify.

[2:08] Fortunately, thanks to the Netlify community, the Netlify CLI supports environment variable imports, so we can just run NTL env-import.env. This tells Netlify to import our test value and our env-var.

[2:27] If I delete this, I can come back out here and restart our project. Now, we can see that we injected this build variable right from our site. If I Netlify, open, and go look at our settings, and see my site settings here, and our build and deploy an environment, we can see that that test value was injected.

[2:59] This is a really fast way for us to test things locally. Then when we're sure that they work, we can run that Netlify and import to get those values up to Netlify really quickly.

[3:09] That means that anybody on this team, anybody I add to this site, will be able to run locally without having to get a .env file, without me having to figure out how to share it, or anything like that. If you have access to this site, you have access to these environment variables. That's a really powerful workflow.

egghead
egghead
~ 45 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today