This is just a quick simple example of course more complex applications will work as well.
Prerequisites
Step 1
Create an account with Vercel if you don't already have one.
Vercel is a cloud platform for static sites and Serverless Functions that fits perfectly with your workflow. It enables developers to host Jamstack websites and web services that deploy instantly, scale automatically, and requires no supervision, all with no configuration.
Step 2
Use npm to install Vercel globally on your computer https://www.npmjs.com/package/vercel
npm i -g vercel
Setup the project
Create a project
mkdir vercel-node-app
cd vercel-node-app
touch index.js
npm init -y
npm i express
Open the project in your code editor and then create a Node server in the index.js
file
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Home Page Route'));
app.get('/about', (req, res) => res.send('About Page Route'));
app.get('/portfolio', (req, res) => res.send('Portfolio Page Route'));
app.get('/contact', (req, res) => res.send('Contact Page Route'));
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server running on ${port}, http://localhost:${port}`));
Create a run script in the package.json
file
"scripts": {
"start": "node index.js"
},
Run the command below to see your Node app working locally in the browser
npm run start
Deploying to Vercel
Make sure that you are in the root folder for your project and then run the command vercel
in your terminal.
Use the project setup settings below as a guide to setup your own project with Vercel.
? Set up and deploy β~/Desktop/username/vercel-node-appβ? [Y/n] y
? Which scope do you want to deploy to? username
? Link to existing project? [y/N] n
? Whatβs your projectβs name? vercel-node-app
? In which directory is your code located? ./
No framework detected. Default Project Settings:
- Build Command: `npm run vercel-build` or `npm run build`
- Output Directory: `public` if it exists, or `.`
- Development Command: None
? Want to override the settings? [y/N] n
Once that is complete it is going to give you some links. The app is NOT going to work yet it is only going to show you the code inside of your index.js
file. You need to create a vercel.json
file and put it in the root folder so that Vercel knows that it is a Node application. And it is very important that your index.js
file remains in the root folder along with your other server side code for the project otherwise your app won't work.
Create a vercel.json
file and put it in the root of your project folder and then add the code below
{
"version": 2,
"builds": [
{
"src": "./index.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}
Now run the command vercel
again to deploy your app. Open the Production link and your app should be working online with full working routes.
Top comments (41)
not working , getting this error
Due to
buildsexisting in your configuration file, the Build and Development Settings defined in your Project Settings will not apply. Learn More: https://vercel.link/unused-build-settings
Try this code in vercel.json.
Brother have you solved this issue?
yeah, Any solution?
You have to remove build script from your package.json file
Thank you for the post! I think the final paragraph is missing one last line. While the vercel CLI will output this, some folks might miss it. Running
vercel
will give you a preview deploy URL to test.If you are happy with the result, run
vercel --prod
to deploy to the production URL.In my case, I needed to use the same base URL to load both the page request and the assets that are included in each page.
Requests:
Files structure:
So, I wrote a
vercel.json
file that takes all the request URLs without file extensions and pass them to theserver.js
file, while the request URLs that have a file extension (like.css, .js, .img
) are taken as static calls to the public directoryI tried to apply the same settings with other src regex and struggled a lot but nothing worked till I tried your solution.
Thank You, so much bro π
I'm happy it helped you :)
This Works Fine for Me with path.resolve(). Thanks !!
{
"builds": [
{
"src": "./app.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/app.js"
}
]
}
Thank you for this. Working perfectly for me.
how to set the port in vercel for it to know which port to serve the app on
Hi I am not sure what you mean I think Vercel assigns a default port that can't be changed. However you can use whatever port you want locally.
What port does it assign by default, like what should I set here app.listen(8000, ()=> {
console.log("app started")
})
I think it uses port 3000. But regardless if you use the below it should be fine. The code
process.env.PORT
means that whatever is in the environment variable PORT, on Vercel's end gets assigned as the port number. Otherwise it uses 3000 or whatever port number you put there.I tried it, but dosent work, just gives me a 404 page on vercel
Hmm is your operating system Linux? Linux is not well supported by Vercel when using this method.
Nope, I'm using plain windows, but I just can't get the app running on vercel for some reason, it's working perfectly well on localhost and even zeet.co (as you have an option to set a port to listen on) but can't figure out vercel
Do you have some github repo or anything which u can link which successfully deploys an express app to vercel
Here Vercel Node App
I checked it out and tried to recreate it but with my app, but still no success :(
here's the repo, if you can point out what mistake i am making : github.com/Krish-Nerkar/genzmafia....
Bonjour Γ tous,
Je suis sous Linux, et j'ai crΓ©e un projet node/express que j'aimerais dΓ©ployer sur Vercel. J'ai suivi toutes vos recommandations mais rien Γ faire, Γ§a m'affiche uniquement l'architecture de mon projet mais pas le rΓ©sultat final ...
Quelqu'un aurait une idΓ©e?
Hé là , je crois que c'est parce que Vercel ne prend pas entièrement en charge Linux lors de l'utilisation de cette méthode. L'utilisation des fonctions sans serveur est une solution possible, mais je ne l'ai pas encore essayée. vercel.com/docs/serverless-functio...
Thanks for the tutorial. Everythings working fine for me first time but when i connect my repository with this deployment and then push new changes, new deployment has white screen and nothing works. Thanks for helping
Hey, this has become deprecated Vercel recommends that everybody uses Serverless Functions now. Follow the documentation and you should be able to get it working. I don't have a tutorial on it yet.
Supported Languages for Serverless Functions
hey there. after you save the last script
vercel.json
run commandvercel --prod
instead of just vercel command as the author says. it works for meHey I just tried it and it's still working. What is your operating system and which browser did you try it in? Are you sure that you followed all of the steps what errors did you get?
Hello, im on linux mint 20, node 12.18.3, chrome 85.0.4183.121. I don't get any erros per se but the deployment page displays my source code as if it was on a directory listing instead of running it
Ok I think that its likely a Linux issue because I have another Vercel guide that uses Python and a few people had problems getting it to work on Linux as well How to deploy a Python/Flask App to Vercel The system architecture is different so I think it changes the paths. You could check out the docs here vercel.com/docs or find a solution on google. If I had Linux I would be able to debug it.
Alright i'll look it up, thanks for the help!
Thanks you help me a lot