Vercel stuck on build state

My Vercel project is stuck on the build state whenever I try to deploy it. My project is a node.js server that uses fastly. The code ends with initializing the server:
fastify.listen(
{ port:3000, host: “0.0.0.0” },
function (err, address) {
if (err) {
console.error(err);
process.exit(1);
}
console.log(Your app is listening on ${address});
setUpDictionary(true);
selectNewWord()
}
);

The code works and prints the expected output, but Vercel remains in the building state. There are no build warnings.

Help would be very much appreciated!

Deployment URL or Custom Domain: https://verbi-six.vercel.app/
Environment (local, preview, production): preview
Project Framework: Node.js
Build Settings: 
  Framework Preset:
  Build Command (if not default): node server.js
  Output Directory (if not default):
  Install Command (if not default): npm install
Node/Runtime Version: 20.x
Package Manager: none
Relevant Packages:

Hi, @matthewl580! Welcome to the Vercel Community :smile:

The issue is that your Node.js server is designed to run continuously, listening on a specific port. Vercel is designed for serverless deployments, which means it expects your application to export handlers for serverless functions rather than running a long-lived server.

What you would need to do is:

  • Restructure your application for serverless deployment

    • Separate server logic from server initialization
    • Remove the fastify.listen() call from your main server file
  • Create a new file

    • Name it api/index.js (or api/index.ts for TypeScript)
    • Export a function that handles requests using your Fastify app
  • Modify your main server file

    • Export the Fastify app instance instead of starting the server
  • Update your package.json

    • Ensure correct build and start scripts are specified

Let us know how you get on!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.