0
0
NodejsHow-ToBeginner · 4 min read

How to Deploy Node.js to Vercel: Simple Steps

To deploy a Node.js app to Vercel, create a vercel.json file or use default settings, then run vercel CLI in your project folder. Vercel automatically detects your Node.js app and deploys it with zero configuration needed for simple apps.
📐

Syntax

Deploying a Node.js app to Vercel mainly involves running the vercel command in your project folder. Optionally, you can add a vercel.json file to customize settings.

  • vercel: The CLI command to deploy your app.
  • vercel.json: Optional config file to define build and routing settings.
  • package.json: Should include a start script to run your Node.js server.
bash
vercel

// Optional vercel.json example
{
  "version": 2,
  "builds": [
    { "src": "index.js", "use": "@vercel/node" }
  ],
  "routes": [
    { "src": "/(.*)", "dest": "/index.js" }
  ]
}
💻

Example

This example shows a simple Node.js server deployed to Vercel. It responds with 'Hello from Vercel!' when accessed.

javascript
const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello from Vercel!');
});

const port = process.env.PORT || 3000;
server.listen(port, () => {
  console.log(`Server running on port ${port}`);
});
Output
Server running on port 3000
⚠️

Common Pitfalls

Common mistakes when deploying Node.js apps to Vercel include:

  • Not having a start script in package.json. Vercel needs this to know how to run your app.
  • Using a fixed port number instead of process.env.PORT. Vercel assigns the port dynamically.
  • Forgetting to install dependencies before deploying.
  • Not initializing the project with vercel login and vercel commands.

Example of wrong and right start script:

json
{
  "scripts": {
    "start": "node index.js"
  }
}

// Wrong: hardcoded port in code
const port = 3000;

// Right: use environment port
const port = process.env.PORT || 3000;
📊

Quick Reference

StepCommand / ActionDescription
1npm init -yCreate a new Node.js project
2npm installInstall dependencies
3Add start script in package.jsonDefine how to start your app
4vercel loginLog in to your Vercel account
5vercelDeploy your app to Vercel
6vercel --prodDeploy to production environment

Key Takeaways

Always use process.env.PORT to let Vercel assign the port dynamically.
Include a start script in package.json to tell Vercel how to run your app.
Run vercel login before deploying to connect your account.
Use the vercel CLI command in your project folder to deploy quickly.
Optionally customize deployment with a vercel.json file for advanced setups.