Express on Vercel

Express is a fast, unopinionated, minimalist web framework for Node.js. You can deploy an Express app to Vercel with zero configuration.

Express applications on Vercel benefit from:

  • Fluid compute: Active CPU billing, automatic cold start prevention, optimized concurrency, background processing, and more
  • Preview deployments: Test your changes on a copy of your production infrastructure
  • Instant Rollback: Recover from unintended changes or bugs in milliseconds
  • Vercel Firewall: Protect your applications from a wide range of threats with a multi-layered security system
  • Secure Compute: Create private links between your Vercel-hosted backend and other clouds

You can quickly deploy an Express application to Vercel by creating an Express app or using an existing one:

To run an Express application on Vercel, create a file that imports the express package at any one of the following locations:

  • app.{js,cjs,mjs,ts,cts,mts}
  • index.{js,cjs,mjs,ts,cts,mts}
  • server.{js,cjs,mjs,ts,cts,mts}
  • src/app.{js,cjs,mjs,ts,cts,mts}
  • src/index.{js,cjs,mjs,ts,cts,mts}
  • src/server.{js,mjs,cjs,ts,cts,mts}

The file must also export the application as a default export of the module or use a port listener.

For example, use the following code that exports your Express app:

src/index.ts
// Use "type: module" in package.json to use ES modules
import express from 'express';
const app = express();
 
// Define your routes
app.get('/', (req, res) => {
  res.json({ message: 'Hello from Express on Vercel!' });
});
 
// Export the Express app
export default app;

You may also run your application using the app.listen pattern that exposes the server on a port.

src/index.ts
// Use "type: module" in package.json to use ES modules
import express from 'express';
const app = express();
const port = 3000;
 
// Define your routes
app.get('/', (req, res) => {
  res.json({ message: 'Hello from Express on Vercel!' });
});
 
app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

Use vercel dev to run your application locally

terminal
vercel dev
Minimum CLI version required: 47.0.5

To deploy, connect your Git repository or use Vercel CLI:

terminal
vc deploy
Minimum CLI version required: 47.0.5

To serve static assets, place them in the public/** directory. They will be served as a part of our Edge Network using default headers unless otherwise specified in vercel.json.

express.static() will be ignored and will not serve static assets.

When you deploy an Express app to Vercel, your Express application becomes a single Vercel Function and uses Fluid compute by default. This means your Express app will automatically scale up and down based on traffic.

Additionally, all Vercel Functions limitations apply to the Express application, including:

  • Application size: The Express application becomes a single bundle, which must fit within the 250MB limit of Vercel Functions. Our bundling process removes all unneeded files from the deployment's bundle to reduce size, but does not perform application bundling (e.g., Webpack or Rollup).
  • Error handling: Express.js will swallow errors that can put the main function into an undefined state unless properly handled. Express.js will render its own error pages (500), which prevents Vercel from discarding the function and resetting its state. Implement robust error handling to ensure errors are properly managed and do not interfere with the serverless function's lifecycle.

Learn more about deploying Express projects on Vercel with the following resources: