Deploying and Managing Environment Variables

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional NextJS Lessons

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
🚨 Work In Progress 🚧
Subscribe for Launch

Deploying and Managing Environment Variables

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Don't Commit Your Env

You don’t normally commit the environment variables into your codebase as that would effectively be the same as directly using the values in your source code.

As an example, in the following project, during development we want STAGE to contain the value development and for production we want it to be called production. Committing a .env file directly into code would mean we would be tied to a particular value.

app/page.tsx
export default function Page() {
const STAGE = process.env.STAGE;

switch (STAGE) {
case "production":
return "We are in production 🚀";
case "preview":
return "You are looking at a preview of the website 🫰🏻";
case "development":
return "Development mode enabled 🔥";
default:
return "Are you sure you provided a valid STAGE? 😢";
}
}

Using your Deployment Pipeline

Because environment variables are not stored in our code, we need a workflow to supply them to our application during deployment.

Exact steps for supplying the environment variables will depend on your CI / CD platform. For this demo we will be using vercel but similar steps exist for any modern system.

You define environment variables in vercel from "Project Dashboard -> Settings -> Environment Variables".

Vercel will automatically provide the values while building and running your application.

Using Development Values Locally

To use the environment variables locally you can pull them to a local (git-ignored) .env.local file. NextJS automatically loads variables from this file.

The process with Vercel is as follows:

# Only on new developer machine setup
npm i vercel -g

# Only on first checkout of repo
vercel link

# Whenever you want updated environment variables
vercel env pull

Unleash the power of Environment Variables

Note that using environment variables to determine the behaviour of our application has another advantage that if we ever want to run a production version of our application locally for testing and debugging, all that we need to do is to pull the production environment variables to our local machine.

To pull environment variables for a particular environment pass the --environment=<name> argument to vercel env pull e.g. for production:

vercel env pull --environment=production
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

You don't normally commit the environment variables into your code base as that would be effectively the same as directly using the values in your source code. As an example, we plan to use an environment variable called stage to change the behavior of our application. For example, if the stage is production, we show a message, we are in production. For preview, we say you are looking at a preview of the website. And for development, we might want to enable some development only features. However, if stage is not one of these values, then we have probably done something wrong and we show an error message as we know we can provide this environment variable using an end file, for example, set the stage to development.

00:34

However, if you were to commit this file, then all of our deployments would behave as if they were in development mode. And since that is not something that we want, we are not going to commit this file because the environment variables are not stored in our code. We need a workflow to supply them to our application during deployment, since we haven't provided the stage environment variable, our production application is not looking very happy. Exact steps for supplying the environment variables will depend upon your CICD platform. For this demo, we will be using versal, but similar steps exist for any modern system with Vercel. Under our project settings, we can find a section dedicated

01:08

to environment variables from the environment dropdown. First we select only the production environment, then we provide stage as the key. And for the value we will use production and we save this particular environment variable. With the production environment variable created, we will repeat the same process for the other environments. We will select preview next and once more we will use stage as the key. And for the value, this time we will provide preview, click save. And finally we will select the development environment. From the dropdown use stage is the key and for the value. This time we will provide development with the values

01:42

provided for the different environments. Let's go ahead and create a new deployment for our production instance. From our deployments page, we can create a new deployment. We select our main branch and click create deployment. This will trigger a new deployment for our production application. And once that deployment is complete, when we visit our production website, again, we now see a healthy message. Remember that we created the stage environment variable for production as well as for preview. So let's kick off a new pull request to get a new preview environment. Once the pull request is created, will automatically spin up a new preview environment.

02:15

And if you visit this preview version of our application, we see a nice message informing us that we are currently in preview mode. So we have the environment variables automatically being provided by our CD pipeline as a part of the deployment. The question you should now have is how do we provide the values during development? Once more, your workflow will depend upon how your deployment platform allows you to access the environment variables locally. But with Versa cell, you can use the Versa cell CLI, which you can install from NPM using NPMI, versa minus G. Once the CLI is installed, we run Versal link

02:48

to link our local repo to the Versal project. In the cloud, we select the project folder along with the team name and the project name in the cloud. This will create a new folder Called Versal, which will contain the configuration that the CLI will use in order to carry out additional commands. Now that we have the Versal CLI configured for our project, all that we need to do in order to pull down environment variables is to run Versal en pull. This will create a new N file called N Local. Additionally, the CLI automatically adds it to GI Ignore, so we don't end up committing it by mistake.

03:22

Next year supports loading environment variables automatically from a file with such a name. Note that while our environment only contains one variable right now, as your projects get more complex, the task of running versa and pull will feel much simpler than having to pass a bunch of variables around in your team. Now that we have this local and file created, if you run our project again, you can see that it was automatically picked up by next yes, which provided the value to our application. And we see our custom message development mode enabled. Note that using environment variables to determine the behavior

03:53

of our application has another advantage that if we ever want to run a production version of our application locally for testing and debugging, all we need to do is to pull the production environment variables to our local machine cel and Full takes an optional argument called Environment. And for the value we provide production, this will pull down the environment variables for our production environment and place them into our end local file. Note that Versal automatically provides a bunch of additional environment variables for the production environment, which is why this file is getting quite big. But within all these environment variables,

04:24

we can still see our custom environment variable called stage with the value production. So now if we locally run our application, again, it'll behave as if it was running in production. And we see the message that we wrote specific to this stage.

Professional NextJS

Professional NextJS

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
🚨 Work In Progress 🚧
Subscribe for Launch