Deploy your TypeScript Express App to Vercel (2024)

Search for a command to run...

No comments yet. Be the first to comment.
1. Introduction In 2017, Vaswani et al. dropped a paper titled “Attention Is All You Need,” and it quietly rewired the entire field of deep learning. Within a few years, its architecture — the Transformer — became the foundation for nearly every mode...

Introduction What happens when you need to store the entire web? That’s the kind of problem Google faced in the early 2000s, and the solution they came up with was the Google File System (GFS). Today I read through the GFS paper — my first real syste...

Introduction When I started working with Oracle SQL, my relationship with databases was simple: they stored my data, and I fetched it when I needed it. SELECT, INSERT, UPDATE, DELETE—that was my comfort zone. Anything beyond that felt like DBA territ...

What started as a simple project to learn GitHub Actions became a potentially powerful conduit for collaboration. NewsCom envisages diverse voices to converge to share insights, experiences, and stories. In this blog, we embark on a journey through a...

Step 1: Organize Files Place your HTML file, CSS file, and assets/js in a dedicated folder. Let's call it website. The folder structure should look something like this. ├── .github/workflows/deploy.yml # we'll talk about this later ├── /W...

Disclaimer: This blog does not discuss express and how to build server logic. This only focuses on deploying the app to Vercel as a Serverless Function.
app instead of listening on a certain port.Export the app in ES6 fashion rather than app.listen()
This
export default app;
Instead of
app.listen(PORT, () => {
console.log("Server listening on port", PORT);
});
api folder for Vercel and set it up.Create an api folder that has an index.ts as follows:
import app from '../app';
export default app;
This imports the app from your root directory (change the path if you have a different setup) and exports it for Vercel.
tsconfig.jsonUpdate tsconfig.json as follows to track the api folder
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"rootDir": "./",
"outDir": "build",
"strict": true
},
"include": ["./api/*.ts"] // -> this is the line you need to update
}
Create an empty Public folder because Vercel looks for it during deployment. We need to keep it even though no static files are to be served.
Create a .gitkeep to track the folder

vercel.json fileThe vercel.json should look like this
{
"rewrites": [
{
"source": "/(.*)",
"destination": "/api"
}
]
}
This directs any API request received anywhere in the app is redirected to the /api folder. The /api folder uses the /api/index.ts which in turn uses the app.ts and on and on and on
package.jsonVercel handles all the transpilation, so we need to overwrite the build command in package.json. Create a new script called vercel-buiild which prevents the typescript compiler from being invoked and instead acts as a dummy placeholder. The script should look like this.
"vercel-build": "echo hello",
The app is now deployable and you can do it from the Vercel console by connecting your GitHub account.
If you have Vercel CLI installed you can check it on your local machine using the command
vercel dev
and deploy using
vercel
My Code: https://github.com/High-on-Bugs/typescript-express-vercel-tutorial
Check this video for a video tutorial:
If you still have any questions/queries you can reach out to me on my LinkedIn / GitHub / Twitter.
Cheers!