
Lesson Description
The "Environment Variables Overview" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:
Scott discusses using typed environment variables in Node.js to securely manage API keys and enable dynamic configuration across local, staging, and production environments.
Transcript from the "Environment Variables Overview" Lesson
[00:00:00]
>> Speaker 1: I'm going to introduce you to one of my first opinions that I don't think many people solve very well, but I do this with every single Node.js project I make, whether it's an API, script, or these days mostly an AI agent Is standardizing and typing out my environment variables What are environment variables Let's talk about that If you haven't taken the Node course or done any system-level OS programming, you might be wondering what an environment variable is, even though you've probably used them in React
[00:00:00]
But the way they're done on the frontend is very much simulated They're not actually environment variables When you use an environment variable on a build system like Vite or Webpack, what's actually happening is that if you do something like in Next.js, where you use `process.env` and add `NEXT_PUBLIC_` to the variable name, what actually happens on the frontend is that it gets substituted out for the actual value of the environment variable
[00:00:00]
If you were to look at the output of that code, you would see the value of the environment variable, which could be dangerous because most environment variables are secrets you don't want people to see You might have prevented GitHub users from seeing it, but you didn't prevent anyone looking at your source code from seeing it That's not how server-side variables work
[00:00:00]
Your server code isn't going to be inspected unless someone gains access to your server An environment variable is essentially a variable injected into the environment, and it's a great way to change configuration and options within your server without having to change the code On the frontend, the equivalent would be something like a feature flag, AB testing, or a headless CMS where you can dynamically change something without making a Git commit or pushing code
[00:00:00]
Environment variables are the genesis of dynamically injecting variables into our environment This is useful for two reasons: first, some things are secrets we don't want to expose in our codebase, and second, we want to reuse the same codebase across many different environments For example, we might want to use the same code locally on our computers, and if we're on a team working on the same app, we'll have multiple environments
[00:00:00]
Staging will have its own environment variables, database, and URL, and production will have its own setup too How would you swap out variables if they were hard-coded If you put the database URL directly in the TypeScript file, what happens when it's on staging or on someone else's computer It would be impossible to manage Some of these variables are on rotation and are dynamic, especially with load balancers or hosting providers where variables are constantly changing
[00:00:00]
An environment variable allows you to dynamically change values in your server without any code changes You typically need a server restart, but you don't need to modify any code.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops