admin管理员组

文章数量:1122832

Im trying to deploy an express app built with typescript to vercel, the deployment succeeds, but there is some misconfiguration with the building of app on vercel i guess. My folder structure is like this.

folder structure

/project-root
├── /src
│   ├── /controllers
│   ├── /middlewares
│   ├── /routers
│   ├── /services
│   └── index.ts        <-- main entry point
│   └── express-app.ts  <-- Express app setup
├── /dist                <-- Transpiled code (generated after build)
├── package.json
└── tsconfig.json

In the vercel dashboard for the deployment, i can see the source and output tabs like this, is this right, i have no idea?? .

I have added the relevent files code, Please help me with this process.

vercel json

{
    "version": 2,
    "builds": [
      {
        "src": "src/index.ts",
        "use": "@vercel/node",
        "config": {
          "typescript": {
            "tsconfigPath": "./tsconfig.json"
          }
        }
      }
    ],
    "routes": [
      {
        "src": "/(.*)",
        "dest": "/dist/index.js"
      }
    ]
  }
  

package json

{
  "name": "pft",
  "version": "1.0.0",
  "main": "dist/index.js",
  "scripts": {
    "build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
    "staging": "ts-node -r tsconfig-paths/register ./src/index.ts --enable-source-maps --no-warnings --loader",
    "dev": "nodemon -r tsconfig-paths/register ./src/index.ts --enable-source-maps --no-warnings --loader"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "bcrypt": "^5.1.1",
    "class-transformer": "^0.5.1",
    "dotenv": "^16.4.5",
    "express": "^4.21.1",
    "jsonwebtoken": "^9.0.2",
    "mongodb": "^6.10.0",
    "reflect-metadata": "^0.2.2",
    "tsconfig-paths": "^4.2.0",
    "tslog": "^4.9.3"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.2",
    "@types/express": "^5.0.0",
    "@types/jsonwebtoken": "^9.0.7",
    "@types/node": "^22.9.0",
    "concurrently": "^9.1.0",
    "nodemon": "^3.1.7",
    "ts-node": "^10.9.2",
    "tsc-alias": "^1.8.10",
    "typescript": "^5.6.3"
  }
}

index ts file

import "reflect-metadata";
require('dotenv').config();
import express, { Application, Request, Response } from "express";

const expressApp = require('./express-app');

const StartServer = async() => {

    // initialize the application server
    const app: Application = express();
    const port = process.env.PORT || 3000;
    
    await expressApp(app);

    // listen to the application server on PORT
    app.listen(port, (): void => {
        console.log(`Express application server running on address => http://localhost:${port}`);
    });
}

if (process.env.VERCEL === '1') {
    module.exports = (req: Request, res: Response) => {
        const app: Application = express();
        expressApp(app);
        app(req, res);
    }
} else StartServer();

本文标签: Deploying an Express app on Vercel with typescriptStack Overflow