How to Deploy a FullStack NodeJs and Express App on Vercel
Tags: webdev, javascript, beginners, programming
Published: 2023-06-05 18:04:39

Hello there, Welcome back to my page. Whether you're a returning visitor or visiting for the first time, Voila! I'm more than thrilled to have you here. Recently, I completed a full-stack project named "Talku-Talku".

Although after sharing it with my peers and the public in general, some developer friends of mine informed me that they struggled with deploying the project. As a result, I decided to write this insightful article on deploying a MERN stack project using Vercel. If you're interested in learning how to deploy a full MERN stack app on Vercel, you're right at the spot. Please join me and let's dive right in.

## Introduction

Deploying a full-stack NodeJs and Express app on Vercel can seem daunting, especially if you're new to web development. But don't worry! With the right guidance and a fun analogy like I always use, you'll be able to deploy your app in no time.

Imagine that your app is like a pizza. You have all the ingredients (code) ready, and now it's time to bake it and serve it to your customers (users). Vercel Here! Let's take it as a pizza oven – it takes care of all the technical details so that you can focus on making a delicious pizza (bakes the pizza so it can be served to consumers or customers).

In this article, we'll walk through the steps of deploying your NodeJs and Express app on Vercel. And we'll cover everything from setting up your environment to configuring your deployment settings. Ace Up, Let's get started!

## First Step: Setting up your environment

Before we dive into deploying our app, let's make sure we have the right tools installed. Here are some of the most important things you'll need:

- Node.js and npm: You can download and install Node.js from the official website. Npm comes bundled with Node.js.
- Git: This version control system will help you keep track of changes to your code. Download Git from the official website.
- A code editor: Choose a code editor that suits your preferences. We recommend Visual Studio Code, which is free and easy to use.
- Express.js: The popular web framework for Node.js, It provides several features such as routing, middleware support, and template engine integration, making it easier for developers to build server-side applications and APIs.

Once you have these tools installed, you are one step ahead and almost ready to go. Open the terminal and navigate to your project folder by typing

```
cd path/to/your/folder
```

Ensure that both the client and server of your application are ready for development, add another terminal for the server, navigate its server path and run both servers, Your server needs to be connected to your database and listening on the specified port(check out my [article](https://vinyldavyl.hashnode.dev/how-to-create-a-database-in-mongodb-atlas-and-connect-your-database-to-your-application-step-by-step) to get more insight on connecting your app to a database), while your client should be accessible through the browser on your localhost.

Now that we have our environment set up, let's move on to deploying your app on vercel.

## Second Step: Kicking off deployment on Vercel

Now that we have our app up and running locally, it's time to deploy it to Vercel. Here's what you need to do:

- Sign up for a Vercel account: If you don't already have a Vercel account, sign up at [https://vercel.com/signup](https://vercel.com/signup).
- Install the Vercel CLI: The Vercel CLI is a command-line tool that makes it easy to deploy your app. Install it by running the following command in your terminal:
 `npm install -g vercel`
- Log in to Vercel

### Deploying the Backend or API of your MERN application:

**Step 1:** Log in to **Vercel** dashboard and click on the “Add New…” button and select the “Project” category. As you can see here, I'm already logged in and I have quite a number of projects with vercel already deployed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xx2bwuiwta7qdrn3veyk.png)


**Step 2:** Import your Git repository after linking your Github/Gitlab/BitBucket account with Vercel, In this case, we would be working with "[talku-talku-v3](https://talku-talku-v3.vercel.app)" for this article.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8l84mshzu4n5at3n6w1.png)

**Step 3:** Configure the backend/server for your MERN application on Vercel

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdn1wb3adcwmt7rpj33w.png)

- Name your project, such as "my-project-backend"
- Select "Other" in the framework option to tailor the setup and customization process to Node.js.
- Indicate the backend directory of your project where Vercel will run the build command.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipe6q5lfa6bbrtl0xrln.png)

- Add any necessary environmental variables, such as **MONGO_ATLAS_URL: my-mongo-atlas-url**. Note: There could be more than one!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f276giatd0063h1dz8m.png)

- Finally, click on the "Deploy" button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlpkqxmcok4a6qqo4mcy.png)

**Step 4:** Very Important, Include the provided **vercel.json** file in your backend/server root directory and then push it to your remote repository. This will enable Vercel configuration.

```
{
    "version": 2,
    "builds": [
        {
            "src": "*.js",
            "use": "@vercel/node"
        }
    ],
    "routes": [
        {
            "src": "/(.*)",
            "dest": "/"
        }
    ]
}
```

**Congratulations!**🎊 Your backend will be up and running within a few minutes after deployment. You should see something [like this](https://talku-talku-v3-server.vercel.app).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0oc1xg6hmyy15k7g72rs.png)

**Quick Note:** As soon as possible, don’t forget to save the new deployment link of your server. "[https://talku-talku-v3-server.vercel.app](https://talku-talku-v3-sever.vercel.app)" in this case, and replace it in your code wherever the local server link was provided.

### Deploying the Frontend of your MERN application:

Quite similar to the backend deployment of our app, we follow the same process.

**Step 1:** Once again, Go to **Vercel** dashboard and click on the “Add New…” button and select the “Project” category.

**Step 2:** Import your Git repository after linking your Github/Gitlab/BitBucket account with Vercel, In this case, we would are definitely working with "[talku-talku-v3](https://talku-talku-v3.vercel.app)" for this article.

**Step 3:** Configure the frontend for your MERN application on Vercel

- Name your project, such as "my-project-frontend"
- Choose "Create-React-App" from the available frameworks to ensure that the configuration and modification procedures are tailored for React.js.
- Indicate the frontend directory of your project where Vercel will run the build command.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hn87lld7yt5fm9fyly9d.png)

- Add any necessary environmental variables, such as **REACT_APP_LOCALHOST_KEY: chat-app-current-user**. Note: There could be more than one and you might want to consider adding **GENERATE_SOURCEMAP: false** for security and performance reasons.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p3c0x4dspbw77yrc87q.png)

- Finally, click on the "Deploy" button once you're done.

**Congratulations!**🎊 Your frontend will be up and running within a few minutes after deployment. You should see something [like this](https://talku-talku-v3.vercel.app).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/faw8it5i8j478sue8pwm.png)

## Finally, Checking and Allowing CORS policy after deployment

**Note:** To solve for CORS policy error if one eventually comes up after deployment of your full-stack app, you can install the cors package and use it to allow cross-origin requests from other domains. This can be done by adding the following code snippet to your backend server:

```
const cors = require('cors');

// Allow all origins
app.use(cors());

// Allow specific origin(s)
app.use(cors({
  origin: 'https://yourdeployedsite.com'
}));
```

Additionally, you should always check the console for errors, including any CORS errors. This will help you identify and fix any issues that may arise.

**Conclusion**

Deploying a full-stack Node.js and Express app on Vercel doesn't have to be difficult. By following the steps outlined in this article and using the pizza analogy at the introduction of this article to get a clearer view, you can easily get your app up and running in no time.

After all that's been said and done, don't forget to check out "[Talku-Talku-V3](https://talku-talku-v3.vercel.app)" on [GitHub](https://github.com/Vinyl-Davyl/talku-talku-v3). Sign up to chat and send media with friends and foes! Share your review and feedback, and leave a star⭐️ if you find something interesting.

Overall, you should by now have a good understanding of the deployment process for a full-stack MERN app using Vercel, including common challenges and best practices. I sure hope so. Please do follow me for more content and updates like this in the future. Until we meet again.

Merci!!