Member-only story
How to Deploy a Node.js Serverless Function to Vercel
Get started with serverless functions

Node.js is an open-source JavaScript runtime environment that can execute JavaScript code on the back-end. If you’re a front-end developer and want to explore back-end development, then Node.js can be a good starting point because you already know JavaScript.
In this post, you’ll learn how to deploy Node.js serverless function to Vercel. Vercel provides serverless runtimes, also known as function as a service (FaaS). There is a myriad of benefits of a serverless function, but the most prominent one is that you’re only running the function when you need them. Let’s get started!
Setup
In this post, I will be using GitHub and Vercel. I’ve created a git repository, you can get it from GitHub and connect it with Vercel.
After you have downloaded the code to your local machine, you can see a JavaScript file called index.js
inside api folder. According to Vercel’s documentation for Node.js, if there is a JavaScript file or a TypeScript file inside the api directory, containing a default exported function, then Vercel will serve it as a serverless function.
Let’s break down the individual ingredients of the index.js
file.
module.exports = (req, res) => {
const name = req.query.name || 'stranger';
res.status(200).send(`Hello, ${name}!`)
}
Here we have a Node.js function. The following line looks for a key called name
in req.query
. If the key is present, the variable name
will contain the name
key’s value, otherwise stranger
. Finally, I’ve set the HTTP status code to 200, and I’m returning the string Hello
, followed by the variable name
and !
at the end. You can read more about advanced Node.js usage from here.
Now, let’s go to Vercel so we can import our project.
Now I’m going to use my git repository name, which is vercel-node.js. Since Vercel can’t find the git repository, I’m going to click Configure GitHub App and add vercel-node.js so that Vercel can access it. After adding the repository, I can access it from Vercel. Now click the Import button.