You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:将TypeScript Node服务器部署到Vercel时遇问题

解决Vercel部署TypeScript Node服务器的NOT_FOUND错误与部署挂起问题

问题描述

  • 部署至Vercel后,访问所有端点均返回Code: NOT_FOUND错误
  • Vercel部署摘要长时间处于运行状态无法完成
  • 服务器在本地环境运行完全正常

当前配置信息

目录结构

Server
   ├─ .env
   ├─ dist
   │  ├─ index.js
   │  └─ model
   │     └─ topic.js
   ├─ index.ts
   ├─ model
   │  └─ topic.ts
   ├─ package-lock.json
   ├─ package.json
   ├─ tsconfig.json
   └─ vercel.json

vercel.json配置

{
    "builds": [
        {
            "src": "dist/index.js",
            "use": "@vercel/node",
            "config": { "includeFiles": ["dist/**"] }
        }
    ],
    "routes": [
        {
            "src": "/(.*)",
            "dest": "dist/index.js"
        }
    ]
}

package.json关键配置

"scripts": {
    "start": "node ./dist/index.js",
    "build": "npx tsc",
    "ts.check": "tsc --project tsconfig.json",
    "add-build": "git add dist",
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "concurrently \"npx tsc --watch\" \"nodemon dist/index.js\""
},
"pre-commit": [
    "ts.check",
    "build",
    "add-build"
]

解决方案

1. 修正vercel.json构建逻辑

当前配置依赖预先生成的dist/index.js,但Vercel构建流程应直接处理TypeScript源码,避免依赖提交的编译产物。修改vercel.json:

{
    "builds": [
        {
            "src": "index.ts",
            "use": "@vercel/node"
        }
    ],
    "routes": [
        {
            "src": "/(.*)",
            "dest": "index.ts"
        }
    ]
}

2. 调整Git提交与构建流程

  • 移除pre-commit中的build和add-build钩子,不要将dist目录提交至Git仓库,让Vercel在平台上自行完成TypeScript编译
  • 登录Vercel控制台,进入项目设置,确认Build Command设置为npm run build,Output Directory设置为dist(若保留原编译输出目录)

3. 检查tsconfig.json核心配置

确保编译输出目录、源码包含范围正确:

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

4. 适配Vercel Serverless环境

Express服务器需调整为适配Vercel的导出方式,避免监听端口(Vercel会自动处理请求转发):

import express from 'express';
const app = express();

// 你的业务路由配置
app.get('/api/topics', (req, res) => {
    // 处理逻辑
    res.json({ data: [] });
});

// 导出Express实例供Vercel使用
export default app;

内容的提问来源于stack exchange,提问作者Pranshu_Taneja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 00:37:49