Deploying a TypeScript Application on VPS: A Step-by-Step Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Deploying a TypeScript Application on a VPS is the Right Choice

Development in TypeScript has become an industry standard thanks to strict typing and improved code readability. However, after development is complete, a key question arises: how to properly deploy the application on a production server? Deploying a TypeScript application on a VPS (Virtual Private Server) is not just about copying files. It is a process that includes compiling TypeScript to JavaScript, setting up the environment, managing processes, and ensuring security.

In this article, we will break down a step-by-step algorithm for deploying a TypeScript application on a VPS. You will learn how to prepare the server, configure the build, run the application using the PM2 process manager, and make it accessible via Nginx. The material is intended for developers who already have a basic TypeScript application (e.g., Express.js or NestJS) and want to deploy it on a remote server.



Step 1: Preparing the VPS and Installing Necessary Software

The first stage of deploying a TypeScript application on a VPS is setting up the server itself. We will use Ubuntu 22.04 LTS as the most stable and popular OS for web servers.

After connecting to the server via SSH, run the following commands:

# Update packagessudo apt update && sudo apt upgrade -y

# Install Node.js (version 18 or higher)curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -sudo apt install -y nodejs

# Check versionsnode -vnpm -v

# Install Gitsudo apt install git -y

# Install PM2 process managernpm install -g pm2

Note: TypeScript does not need to be installed globally on the server. It will only be used during the project build stage, which can be performed either on the local machine or on the server via CI/CD scripts.

It is also recommended to set up a basic firewall:

sudo ufw allow OpenSSHsudo ufw allow 'Nginx Full'sudo ufw enable


Step 2: Cloning the Project and Building TypeScript

Now that the server is ready, let's transfer the TypeScript application code. The most common method is cloning the repository via Git.

# Clone the repositorygit clone https://github.com/your-username/your-project.gitcd your-project

# Install dependenciesnpm install

# Compile TypeScript to JavaScriptnpx tsc

Important: make sure the output folder is correctly specified in the tsconfig.json file. Usually this is dist or build:

{  "compilerOptions": {    "target": "ES2020",    "module": "commonjs",    "outDir": "./dist",    "rootDir": "./src",    "strict": true,    "esModuleInterop": true  },  "include": ["src/**/*"],  "exclude": ["node_modules", "dist"]}

After successful compilation, JavaScript code ready to run will appear in the dist folder. Check that the application starts correctly:

node dist/index.js


Step 3: Running via PM2 and Configuring Auto-Start

Simple startup via node is not suitable for production — if the SSH connection is interrupted, the process will stop. For deploying a TypeScript application on a VPS, we use PM2, which provides:

  • Automatic restart after a crash
  • Error and stdout logging
  • Clustering (running on multiple CPU cores)

Create an ecosystem.config.js file in the project root:

module.exports = {  apps: [{    name: 'my-ts-app',    script: './dist/index.js',    instances: 'max',    exec_mode: 'cluster',    env: {      NODE_ENV: 'production',      PORT: 3000    },    error_file: './logs/err.log',    out_file: './logs/out.log',    merge_logs: true  }]};

Start the application via PM2:

pm2 start ecosystem.config.jspm2 savepm2                

Blogs

Book Recommendations