如何解决NestJS部署至Vercel时出现的错误?
Hey there, let's break down why your deployment is failing and how to fix it. Looking at your now.json config and the tech stack you're using, there are a few key issues to address—especially around Vercel's serverless environment limitations and your build setup.
1. Fix Your Build Configuration
First up, Vercel might not be properly compiling your NestJS project before trying to deploy dist/main.js. Here's how to adjust your now.json:
- Update the
usefield to use the current Vercel Node.js builder (@vercel/nodeinstead of the deprecated@now/node) - Add a root-level
buildCommandto ensure Vercel runs your NestJS build script - Make sure your
package.jsonhas a validbuildscript like"build": "nest build"
Modified now.json:
{ "version": 2, "name": "nestjs-now", "buildCommand": "npm run build", "builds": [ { "src": "dist/main.js", "use": "@vercel/node" } ], "routes": [ { "src": "/(.*)", "dest": "dist/main.js" } ] }
Also, double-check your main.ts to listen on the port provided by Vercel (instead of hardcoding 3000):
await app.listen(process.env.PORT || 3000);
2. Socket.io Doesn't Work with Vercel Serverless Functions
This is probably the biggest blocker. Vercel's serverless functions are stateless and short-lived (max execution time around 10 seconds for free tiers), which conflicts with Socket.io's need for persistent WebSocket connections. When a WebSocket tries to connect, the serverless function will terminate after the initial request, breaking the connection.
Solutions for Real-Time Functionality:
Option 1: Host Socket.io separately
Deploy a dedicated Node.js server for Socket.io on a platform that supports long-lived connections (like Heroku, DigitalOcean App Platform, or AWS EC2). Your Vercel-hosted NestJS API can communicate with this server via HTTP requests when needed.Option 2: Switch to serverless-compatible real-time tools
Use services like Pusher, Ably, or Firebase Realtime Database—these work over HTTP-based protocols that play nicely with Vercel's serverless environment, no persistent connections required.Option 3: Try Vercel Edge Functions
Vercel recently added limited WebSocket support for Edge Functions. You'll need to refactor your Socket.io setup to run on Edge Functions instead of a traditional Node.js server, but note that Edge Functions have their own constraints (like limited dependencies and execution time).
3. Verify GraphQL & REST API Setup
Before tackling Socket.io, test deploying just your REST and GraphQL endpoints to confirm they work:
- Enable CORS in
main.tsto avoid cross-origin errors from your frontend:app.enableCors({ origin: process.env.FRONTEND_URL || "*", // Lock this down in production credentials: true }); - For GraphQL, ensure your
GraphQLModuleis configured correctly for production (e.g., disable playground if needed, but keep the API endpoint active):GraphQLModule.forRoot({ autoSchemaFile: true, playground: process.env.NODE_ENV !== "production", introspection: process.env.NODE_ENV !== "production" });
Testing Tip
Temporarily comment out all Socket.io-related code in your NestJS app, deploy the stripped-down version to Vercel, and check if your REST and GraphQL endpoints work. If they do, you know the core deployment setup is correct, and you can focus on resolving the Socket.io hosting issue.
内容的提问来源于stack exchange,提问作者Yuji

