Can i deploy nodejs application along with database for free?
Disclaimer: If you have a full Node.js application with database, the following solution might not be the best option because it is optimized for serverless functions and static sites rather than traditional server applications
I used the AWS EC2 to deploy most of my node js application back when i was just started. It was awesome for me as i learned a lot out about server configuration. But as the number of my pet project started to went up, it started to put the dent on my pocket as well. I was looking for the alternative solution where i can deploy the node js application along with the database. Well the very blog you are reading now is the result of that research.
Vercel (Hobby)
Vercel was not new to me, i used it deploy my most of the Frontend project [React/Nextjs ] but never in once it crossed in my mind that i can use the vercel to deploy the backend also, always thought it was just packaged for the Frontend. Ya, i am that stupid. I only started looking for the free alternative for my student, as they can’t afford the AWS EC2 in the time of writing blog. And my Friend “Google” quickly suggest me to start with the vercel for the node js application. Well now we are here, i will try to explain how you can deploy your application for free in Vercel.
Technically there are 2 different ways you can deploy node js. Trust me, both are very easy to get started.
Technique 1: GITHUB
Technique 2: Vercel CLI
Before we move on to any of the technique, lets first setup the vercel configuration on the node js application. Make sure its on the root of the application. Example file structure: Create the file named: vercel.json
|_package.json
|_node_modules
|_vercel.json
|_others..
Inside the vercel.json file: add the following code.
{
"version": 2,
"builds": [
{
"src": "./index.js", //put path of your root file
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}
This is the bare minimum configuration you need to have for the nodejs application. Basically it tell the vercel how to build and which routes to expose. You can learn more about the configuration from here, but for now its all good.
Assumption: you already have the vercel account.
Technique number 1: Github
- Once logged in to the vercel, click on New Project.
- Select Import from Git repository.
- Choose GitHub and grant Vercel access to your repository.
- By default, Vercel will auto-detect your Node.js application.
- Make sure your
package.json
has the correct scripts, such as:{ "scripts": { "start": "node index.js" } }
- Once you select the github project and after all the permission is provided, vercel will do the rest of the magic for you. After a while you will get your Live URL.
Thats it!
Technique 2: Vercel CLI
- First make sure you have the vercel setup on your machine You can check with
vercel --version
- If not installed already: try the following command
npm i -g vercel
- Once vercel is setup, from your code directory
Start the deployment process the command
vercel
in terminal - You will be asked following question
? Set up and deploy “~/projects/ticket/server”? [Y/n] y
? Which scope do you want to deploy to? roshanshrestha123
? Link to existing project? [y/N] n
? What’s your project’s name? server
? In which directory is your code located? ./
? Want to modify these settings? [y/N] n
Once everything is setup: Vercel should do its magic and provide the link.
Thats it! Nodejs deployed and server for free. Good luck with your project.