How to deploy a Next.js app to production
Tags: webdev, javascript, nextjs, react
Published: 2022-12-05 00:05:28

by [Sage Osoro](https://blog.openreplay.com/authors/sage-osoro)

Next.js is a robust framework for building static and Server-Side Rendering (SSR) applications. With its impressive Search Engine Optimization (SEO), it is no wonder it has become a favorite for developers.

It's one thing to create a Next.js application and another to deploy it to a website and take advantage of its SEO and SSR capabilities. After reading this article, you should be able to deploy your Next.js application to production using the Netlify and Vercel Command Line Interfaces(CLIs).

This article assumes that you have basic experience with using the following:

- GitHub
- React.js
- Next.js

If you have yet to gain experience with Next.js and want to learn more about the framework, follow this [link](https://medium.com/@sageosoro/why-you-should-use-next-js-in-all-your-react-projects-a8e634bba4b4) to my Next.js article.

## Preparing Application for Deployment
Next.js has three helpful script commands to help deploy your application to production. They are "next build", "next start", and "next export".

You should run the "next build" command before "next start" or "next export". After running the command, it creates an optimized version of your app for production, and then it saves this version to a newly created folder called ".next". 

The "next start" command starts a Node.js server in production mode that supports static and server-side rendered pages. The "next export" command, however, exports all your pages to static HTML files and saves the files to a newly created folder called "out". This folder is the equivalent of the "build" folder found in traditional React applications. By default, Next.js does not provide the "next export" command in your project. To use the command, add `"export": "next export``` in the scripts of your "package.json" file like so:

```javascript
"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "export": "next export",
    "lint": "next lint"
},
```

With these essential points in mind, let's proceed to have your application deployed. First, create a new GitHub repository and push your application to that repository. Next, I will show you how to deploy your application to the popular hosting platforms Netlify and Vercel.

## Deploying to Netlify
We will be deploying to Netlify with its Command Line Interface(CLI). If you don't already have a Netlify account, follow this [link](https://app.netlify.com/signup) to create one and select GitHub as your sign-up method:

![1](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image01.png)

Now, install the CLI package into your terminal with the command below:
```
npm install -g netlify-cli
```
Next, log into your Netlify account from your terminal with:
```
ntl login
```
You will be redirected to this page, where you must authorize the CLI to access Netlify on your behalf. Click on "Authorize":

![2](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image02.png)

Great! Now that you are logged in to Netlify, create a new site for your application by running the command:
```
ntl init
```
After running the command, select "Create & configure a new site".

![3](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image03.png)

Now give the site a name. This name will be added before the domain "netlify.app". After setting a name for your site, Netlify will provide information about your new site. However, the site is not yet active:

![4](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image04.png)

Authorize the Netlify CLI to add an SSH key to your GitHub repo. Then, select "Authorize with GitHub through app.netlify.com".

![5](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image05.png)

After successful authorization, you will be redirected to localhost:3000 with this message:

![6](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image06.png)

Back in the terminal, you will get asked to build and deploy the Next.js application in production mode as a server-side rendering or static website. For an SSR website, type in the command `npm run build` and click Enter. After this, specify the directory to deploy. Type ".next":

![7](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image07.png)

Your site is now active on the Netlify server. To visit your deployed Next.js website, type in the name you gave to the project along with "netlify. app", like, e.g., "my-next-application.netlify.app".


<h2>Open Source Session Replay</h2> 

_<a href="https://github.com/openreplay/openreplay" target="_blank">OpenReplay</a> is an open-source, session replay suite that lets you see what users do on your web app, helping you troubleshoot issues faster. OpenReplay is self-hosted for full control over your data._

![OpenReplay](https://blog.openreplay.com/banner-blog_1oYPGT.png)

_Start enjoying your debugging experience - <a href="https://github.com/openreplay/openreplay" >start using OpenReplay for free</a>._




## Deploying to Vercel
Since Vercel is the creator behind Next.js, it makes sense that the hosting platform would make it super easy for developers to deploy their Next.js applications. 

You must have a Vercel account before using the Vercel CLI in your workspace. If you don't already have an account with Vercel, follow this [link](https://vercel.com/signup) to create one and select GitHub as your sign-up method.

Install the Vercel CLI by running the command:
```
npm install -g vercel
```
Log in to the CLI with the command:
```
vercel login
```
Select a login method. Use the same method you used while signing up for Vercel.

![8](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image08.png)

After clicking on a login method, you will be redirected to a page with this message displayed:

![9](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image09.png)

Now that you've logged into Vercel from the CLI, run the command below to deploy:
```
vercel
```
Input "Y" into the terminal to confirm you want to deploy the project from your current directory.

![10](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image10.png)

Select the Vercel account (if you have more than one) you want your project deployed to.

![11](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image11.png)

Input "N" to create a new project on Vercel.

![12](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image12.png)

Give the project a name. This will be added before the domain "vercel.app".

![13](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image13.png)

Specify the location of your code by inputting `./`, which is the root of the project directory.

![14](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image14.png)

Vercel can detect that the application is a Next.js project. Input "N" to use the default build commands for running Next.js.

![15](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image15.png)

And that's it! Vercel is that easy to deploy. To visit your new Next.js website, type in the name you gave to the project and "vercel.app" into your browser;  e.g., "my-next-application.vercel.app".

Vercel also creates a new ".vercel" folder with a package.json file. This file contains the origin and project IDs for your deployed Next.js application. The IDs help in pushing changes from your local machine to Vercel. To use them, run the command:
```
vercel --prod
```

## Adding Environment Variables
To add your environment variables to the deployed application, run the following commands on your terminal:

* For Netlify run:
```
netlify env:set YOUR_ENV_VAR "VALUE"
``` 

* For Vercel run:
```
vercel env add YOUR_ENV_VAR
```

You will be asked for the value of the environment variable and the environment to add the variable to:

![16](https://blog.openreplay.com/images/how-to-deploy-a-nextjs-app-to-production/images/image16.png)

You have now successfully added environment variables to your deployed Next.js application from the comfort of your command line :).

## Conclusion

Netlify and Vercel are great platforms to host your Next.js application. Vercel, in particular, always adds new features to make the deployment process easier for developers since Next.js is their product.

> A TIP FROM THE EDITOR: For more options, consider our [Alternatives To Netlify For Deploying Front-End Apps](https://blog.openreplay.com/alternatives-to-netlify-for-deploying-front-end-apps/) article.



[![newsletter](https://blog.openreplay.com/newsletter_Z1P0UIP.png)](https://newsletter.openreplay.com/)