Skip to main content

Command Palette

Search for a command to run...

How to Deploy a FullStack NodeJs and Express App on Vercel

A full guide on deploying your fullstack MERN app on Vercel, covering everything from deploying your Express API to connecting it with the frontend

Updated
6 min read
How to Deploy a FullStack NodeJs and Express App on Vercel
V

Software Engineering! knife talk only🧘

JavaScript, Typescript, Golang, Solidity, React.js, Vue.js, Node.js and the web!

Come and explore this world of the software development Industry with me!

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 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.

  • 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.

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" for this article.

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

  • 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.

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

  • Finally, click on the "Deploy" button.

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.

Quick Note: As soon as possible, don’t forget to save the new deployment link of your server. "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" 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.

  • 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.

  • 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.

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" on GitHub. 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!!

N
nasir1y ago

very help full

O

It's an awesome and very helpfull article.

7
V

Thanks Osman

O

Hi, and thanks for this awesome article.

I have this MERN app I've deployed to Vercel (my GitHub username is DragonOsman. Please go to my GitHub page and go to the personal-library repo; the deployment link is on the right-hand panel). I setup CORS and a proxy (proxy key in client directory package.json that points to the server URL), and the paths and methods are all correct as far as I can see, but I still get a 405 error when I try to send a POST request to my login route.

Source code repository is on my personal-library repository (DragonOsman/personal-library). Any help is appreciated. [I have to describe like this because I can't post links yet.] Thanks.

Edit: I removed proxy and only kept CORS, which I'm doing like this:

const CLIENT_URL = "<client_url>";

app.use(cors({
  origin: [`${CLIENT_URL}`, `${CLIENT_URL}/`],
  methods: ["GET", "PUT", "POST", "DELETE"],
  credentials: true
}));

but I still have a CORS error in my browser's console that says:

Access to fetch at '<backend_login_url>' from origin '<client_url>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I added res.setHeader("Access-Control-Allow-Origin", [CLIENT_URL,${CLIENT_URL}/]); on all my routes, right at the start of the function code block, and I also added app.options("*", cors()); (tried it both before and after my app.use(cors({...})) call). But that error won't go away. What should I do?

7
V

Okay, Hi DragonOsman,

I understand the frustration you're facing with the CORS issue in your MERN app deployed on Vercel. It's great that you've taken steps to set up CORS and proxy settings to allow communication between your frontend and backend.

Sorry for replying this late. But I would try and help as much as I can.

First, make sure that your CORS configuration is correct in both your server and client code. It seems like you've defined the CLIENT_URL and added it to the origin field in your app.use(cors(...)) middleware, which is correct. Yeah, but However, the issue might lie in how the headers are being set and how preflight requests are being handled.

I suggest to resolve this issue you check your Server Headers: Make sure that your server is actually sending the Access-Control-Allow-Origin header with the correct value (CLIENT_URL). It should be sent as a response header when the server receives an OPTIONS request.

Also Check the Handle Preflight Requests: The app.options("*", cors()); line is correct, and it should come before the app.use(cors({...})) middleware. This handles preflight requests, which are used to check the server's CORS policy before making the actual request. Ensure that this setup is in place.

Check, CORS Configuration too Osman. Verify that there are no typos or syntax errors in your CORS configuration. Double-check the URLs and ensure they match your frontend and backend URLs. And even the deployed ones. If you have them deployed on vercel already.

Check your Vercel Configurations and Network Tab Debugging: Check if Vercel has any specific CORS settings that you need to configure. Some hosting platforms have their own CORS configurations that can affect your app. Use your browser's developer tools to inspect the network requests. Check the request and response headers to ensure that the expected CORS headers are present.

This might help, Remember, CORS issues can be tricky, but with careful debugging and attention to detail, you'll likely find a solution. Best of luck, and I hope your app's functionality will be up and running smoothly soon! 🚀 cheers.

N

Nice one!

8
N

Nice one!

9
V

Thanks, Glad you liked it

S

Wow, Thanks alot I'm guessing the vercel.json is quite important for the backend deploy

6
V

Yeah!! Glad you liked this. Yeah! really really important. more or less like a corner stone👉👈

I

Thanks for this

3
V

My pleasure, glad you like

A

Thank you for this.

5
V

My pleasure! Happy you liked this

1