NextJS Environment Variables Masterclass

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
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
🚨 Work In Progress 🚧
Subscribe for Launch

NextJS Environment Variables Masterclass

Subscription Required

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

Core Concept

Using Environment Variables is a standard practice for modern software development.

It is common to have multiple deployed instances of our application also called environment. For Example:

  • dev for continuously deploying new code
  • staging which is a replica of production for testing before production deployment
  • production which is the instance of our application used by our commercial clients.

Storing information for accessing external resources like databases directly in our code ties all these environment to a specific instance of the external resources.

As you can imagine this will be a problem for a number of reasons e.g. the production database will collect development junk and this also means that anyone with access to the source code will be able to access the production database without any peer review.

Moving this information into environment variables allows us to use the same code, deployed to different environments with their own environment variables which allows us to scale our external resources to multiple instances.

Note that this is not the only benefit of using environment variables, but its enough to motivate their usage.

Using Environment Variables with NextJS

The good news is that NextJS comes with built in support for loading environment variables, without need to use third party packages.

To provide environment variables create a .env file in your project and provide the variables as key value pairs. You can then access them from your code using the NodeJS Global process.env.

This is demonstrated in the below example.

.env
app/page.tsx
DB_URL=postgresql://username:password@neon.tech

Categories of Environment Variables

There are actually two categories of environment variables.

  • Server Only: The first category are the ones intended for use on the server. These are things that we need to keep secure. An example of a server only environment variable is database access url.
  • Public: The second category are the ones that we want to be available for access from the frontend code of our application which also makes them accessible to the users because the frontend code is shipped to the browser. An example of a public environment variable is the tracking id used by browser analytic services.

Making PUBLIC Environment Variables

By default all environment variables are considered server only. In order to make a public environment variable you need to explicitly prefix its name with NEXT_PUBLIC_.

This is demonstrated in the following example, client code can only access public environment variables and the server code can access all environment variables.

.env
app/server/page.tsx
app/client/page.tsx
DB_URL=postgresql://username:password@neon.tech
NEXT_PUBLIC_TRACKING_ID=1337
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

Using environment variables is a standard practice for modern software development. A question you might have is, why do we use environment variables? It is common to have multiple deployed instances of our application, also called environments. For example, a dev environment for continuously deploying our code staging, which is a replica of production for testing before production deployment, and of course production, which is the instance of our application used by our commercial clients. Storing information for accessing external resources like databases directly in our code ties all these environments

00:33

to a specific instance of the external resource. As you can imagine, this will be a problem for a number of reasons. For example, the production database will collect development junk, and this also means that anyone with access to the source code will be able to access the production database without any peer review. Moving this information into environment variables allows us to use the same code deployed to different environments with their own environment variables, which allows us to scale our external resources to multiple instances. Note that this is not the only benefit of using environment variables,

01:05

but even this by itself is enough to motivate the usage. The good news is that next JS comes with built-in support for loading environment variables, and we don't need to install any additional packages. We can provide environment variables in a dot n file, so we create this within the root of our project, and here we provide key value pairs where the key will be the environment variable name followed by the equal sign and the value. We can use these environment variables within our code. For example, within a server page, we are going to render out the D-B-U-R-L in a simple diviv. To read the value of an environment variable,

01:37

we use the global process N, which is provided by node js. When we run our next J server, it'll automatically pick up that an environment configuration file do NV has been provided. When we run our application in the browser, you can see that process dot n do DB URL has been replaced by the value that we provided in our environment file with next js. There are actually two categories of environment variables. The first category are the ones intended for use on the server. These are the things that we need to keep secure. An example of a server only environment variable is database access, URL.

02:09

The second category are the ones that we want to be available for access from the front end code of our application, which also makes them accessible to users because the front end code is shipped to the user's browser. An example of a public environment variable is the tracking ID used by browser analytics services. Note that the public environment variables are also available for access on the server. However, the server only environment variables cannot be directly accessed from client side code, which is why the categories are called server only versus public. By default, all environment variables are considered server only.

02:42

In order to make a public environment variable, you need to explicitly prefix its name with next public. In addition to our server only environment variable, we create a new public environment variable called tracking id, and to ensure that it's public, we prefix it with the next public. As we know, a server component Only runs in a server context, which means that we can access D-B-U-R-L as well as next public tracking. Id note that normally you wouldn't log server only environment variables in a client response, but this is only for demonstration. Effectively, it is the same as creating an HTP endpoint and sending a server only value to the client.

03:16

Next, we jump into a client context by creating a client component as specified by the Hughes client directive. Within our client component, we use the same code that we wrote in our server component. We log out the D-B-U-R-L as well as the public tracking id. Since this is a client component, next J will only provide us with a public tracking ID and the D-B-U-R-L over here will actually be undefined. So let's verify that by using the browser. When we visit our server component, you can see that both the environment variables were provided. However, when we visit our client component, DB URL is undefined and only the public environment variables are accessible.

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
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
🚨 Work In Progress 🚧
Subscribe for Launch