DEV Community

Ada Cheng
Ada Cheng

Posted on • Edited on

Deploy a Serverless Node.js application to Vercel in 5 minutes

Table of Contents

Introduction

Deploying a Serverless Node.js application to Vercel can be easily done by just adding a vercel.json configuration file to your project root directory.

Here I will show you how it can be achieved.

Please note that Vercel platform is serverless and designed for static frontends and Serverless Functions. There is no running server.

Add vercel.json file

First of all, add a vercel.json file in the root directory of your existing Node.js project. Add and commit the file to the Git repository.



{
"version": 2,
"name": "music-album",
"builds": [
{ "src": "app.js", "use": "@vercel/node" }
],
"routes": [
{ "src": "/(.*)", "dest": "/app.js" }
]
}
Enter fullscreen mode Exit fullscreen mode




Create a Vercel project

  1. Login Vercel.

    If you don't have an account yet, you can sign up for a free account.

  2. Click the "+ New Project" button on the Overview tab.

    New Project

  3. Import Git Repository by selecting the Git provider. GitHub, GitLab and Bitbucket are available.

    Import GIt Repo

  4. Import Git Repository.

    Import GIt Repo

  5. Configure the project.

    1. Give a Project Name.
    2. Click the "Deploy" button. The deployment should be triggered.

    Configure Project

  6. Check the deployment status.

    Check deployment

  7. Click the link under DOMAINS to view the application in browser.

    View deployment

Conclusion

Congratulations! Your Node.js application is now configured to be deployed to Vercel easily by adding a configuration file to the project.

References

  1. Source code in GitHub
  2. Live demo at Vercel
  3. Project config with vercel.json

Top comments (20)

Collapse
 
letsbsocial1 profile image
Maria Campbell

Looking forward to doing this for several apps I currently have on Heroku but want to remove them from there. The only thing is that I also use environment variables there. How would I add them on Vercel? Because they are configured and named differently there. At least when creating a next.js app. Would that also apply with a regular Node.js app?

Collapse
 
adafycheng profile image
Ada Cheng

Hi Maria. Please note that Vercel platform is serverless and designed for static frontends and Serverless Functions.

Example of static frontend using React.js: portfolio-website-react-adafycheng...

Example of Serverless Functions using Node.js: music-album-liart.vercel.app/

Collapse
 
letsbsocial1 profile image
Maria Campbell

Hi Ada, I got my first express transformed to Next.js app up on vercel.com! So much shorter a process. So much less code. Read to tackle my other projects! Here is the link to the live site: trump-talks-nextjs.vercel.app/ and the link to the repo: github.com/interglobalmedia/trump-...

Collapse
 
letsbsocial1 profile image
Maria Campbell

Hi Ada. I see. I will check it out. Thanks! Because I am taking off my Node.js (no dbs) projects from Heroku and want to figure out how I can deploy them on vercel.com. But maybe then it might not be the best alternative. I will see. Thanks again for the info!

Collapse
 
letsbsocial1 profile image
Maria Campbell

Of course. I just have to convert the apps to Next.js projects. That will be good practice! Thanks again.

Thread Thread
 
adafycheng profile image
Ada Cheng

Great! Just let me know the result. I'm interested to know whether Next.js application can be deployed to Next.js.

Thread Thread
 
letsbsocial1 profile image
Maria Campbell

I will totally will let you know. And will even link to the live site here. Thanks again!

Collapse
 
socr102 profile image
Eric

great~
I have deployed the node.js on the Vercel

Collapse
 
jameshubert_com profile image
James Hubert

I think you meant vercel.json instead of version.json above? Seems to get picked up by Vercel this way. Just a small nit :) Thanks for the helpful post

Collapse
 
adafycheng profile image
Ada Cheng • Edited

Oh yes, you're right. Thank you for the correction! :)

Collapse
 
hack12web profile image
Derek Claude

everything works fine, I've this error: {
"code": "ER_ACCESS_DENIED_ERROR",
"errno": 1045,
"sqlMessage": "Access denied for user 'uj8vm0xflmhp3uve'@'3.215.23.141' (using password: YES)",
"sqlState": "28000",
"fatal": true
}
when i tried to get something. can you help?

Collapse
 
kevinqtogitty profile image
Kevin To

Hi Ada,

I'm using typescript so Im guessing I have to point the filepath for build/routes towards the distribution folder? And could you explain what the difference is between the build and route configuration is?

Thanks!

Collapse
 
adafycheng profile image
Ada Cheng

Hi Kevin. For the build configuration, it allows me to run the .js files using Node.js. Without this, they are just served as plain text files. For the route configuration, I just forward all requests to the /app.js for processing.

Collapse
 
filipedanunes profile image
Filipe Nunes

hey @ada i was trying to deploy github.com/nightscout/cgm-remote-m... using ur tutorial but something is missing. can you help on this?

Collapse
 
adafycheng profile image
Ada Cheng • Edited

Hi Filipe. I just checked the documentation from Vercel. Please note that Vercel platform is serverless and designed for static frontends and Serverless Functions. There is no running server. That's why your application cannot run on Vercel.

My article does not emphasize this point. I'll update it. Sorry for any confusion and misleading information it may have caused.

vercel.com/guides/npm-run-start-no...

Collapse
 
adafycheng profile image
Ada Cheng

Sure, what's the problem?

Collapse
 
dariusgarcia profile image
Darius Garcia

Thanks for your help. I had to switch from Heroku hosting my Node.js app since their services aren't free anymore.

Collapse
 
remireci profile image
remireci • Edited

Great tutorial, it helped me a lot. Thanks!

Collapse
 
remireci profile image
remireci

Great tutorial. It helped me a lot. Thanks!

Collapse
 
hafizip00 profile image
Inayat

there is CORS problem with this approach can you help me about this?