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

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

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

\n\n

Developing in TypeScript is a modern standard for creating reliable and scalable web applications. However, after writing the code, the question arises: how to properly deploy it on a real server? In this article, we will cover the complete deployment cycle of a TypeScript application on a VPS (Virtual Private Server). You will learn how to set up the environment, compile TypeScript into JavaScript, organize auto-start using PM2, and configure a reverse proxy through Nginx. The material is intended for developers who already have a basic understanding of TypeScript and Linux.

\n\n

1. Preparing the VPS and Installing Necessary Software

\n\n

The first step is preparing the server. We will use Ubuntu 22.04 LTS, as it is one of the most stable and popular operating systems for VPS. Connect to the server via SSH:

\n\n
ssh root@your-server-ip
\n\n

Update the package manager and install Node.js (recommended LTS version) along with the npm package manager:

\n\n
apt update && apt upgrade -y\ncurl -fsSL https://deb.nodesource.com/setup_20.x | bash -\napt install -y nodejs git nginx
\n\n

Verify the installation:

\n\n
node -v   # v20.x.x\nnpm -v    # 10.x.x
\n\n

Now, install TypeScript and PM2 (a process manager for Node.js) globally:

\n\n
npm install -g typescript pm2
\n\n

Important: TypeScript is compiled into JavaScript, so it is not necessary to have TypeScript itself in the runtime on the server — it is enough to compile the code locally or on the server before running. However, a global installation of ts-node can be useful for debugging.

\n\n

2. Compiling TypeScript and Configuring package.json

\n\n

Assume your application has the following structure:

\n\n
my-ts-app/\n├── src/\n│   ├── index.ts\n│   ├── routes.ts\n│   └── utils.ts\n├── package.json\n├── tsconfig.json\n└── .gitignore
\n\n

The tsconfig.json file should be configured for a production build. Example configuration:

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

Add build and start scripts to package.json:

\n\n
{\n  "name": "my-ts-app",\n  "version": "1.0.0",\n  "scripts": {\n    "build": "tsc",\n    "start": "node dist/index.js",\n    "dev": "ts-node src/index.ts"\n  },\n  "dependencies": {\n    "express": "^4.18.2"\n  },\n  "devDependencies": {\n    "typescript": "^5.3.3",\n    "@types/express": "^4.17.21",\n    "ts-node": "^10.9.2"\n  }\n}
\n\n

Now, when you run npm run build, TypeScript compiles the files from the src folder into the dist folder. Example of a simple Express server (file src/index.ts):

\n\n
import express, { Request, Response } from 'express';\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\napp.get('/', (req: Request, res: Response) => {\n  res.send('TypeScript application is running on VPS!');\n});\n\napp.listen(PORT, () => {\n  console.log(`Server started on port ${PORT}`);\n});
\n\n

3. Cloning the Repository and Building on the Server

\n\n

Copy your application to the server. The most convenient way is via Git. If your code is stored on GitHub/GitLab, run on the server:

\n\n
cd /var/www\ngit clone https://github.com/your-username/my-ts-app.git\ncd my-ts-app
\n\n

Install dependencies and perform the build:

\n\n
npm install\nnpm run build
\n\n

After this, compiled .js files will appear in the dist folder.

Blogs

Book Recommendations