From 19ce4f05c7adb74a265dc53289abca6ff9bfa8b0 Mon Sep 17 00:00:00 2001 From: Barry Prendergast Date: Sun, 25 Oct 2026 17:39:23 +0100 Subject: [PATCH 1/2] Create the Express app in app.js so Vercel recognises the project The first real Vercel builds failed with "No entrypoint found which imports express. Found possible entrypoint: app.js": Vercel scans the entry file itself for an Express import, and ours created the app in src/. app.js now imports Express, creates the app and passes it to createSite(). Nothing else changes: it is still exported for Vercel, and still listens on a port when run anywhere else. Co-Authored-By: Claude Sonnet 5.5 --- app.js | 10 ++++++++-- src/http/app.js | 8 ++++++-- src/site.js | 7 ++++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index d749732..c321a56 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,18 @@ // Entry point. On Vercel this file is the function: it exports the Express app // and Vercel handles requests. Anywhere else (`npm run dev`, `npm start`) it // listens on PORT as a normal server. +// +// Vercel only recognises an Express project if this file itself imports and +// creates Express, which is why the app is made here rather than in src/. +import express from 'express'; import { ConfigError } from './src/errors.js'; import { createSite, serve } from './src/site.js'; +const app = express(); + let site; try { - site = await createSite(); + site = await createSite({ app }); } catch (error) { // A readable message instead of a stack trace; on Vercel, rethrow so the deploy's logs show it. if (!(error instanceof ConfigError) || process.env.VERCEL) throw error; @@ -16,4 +22,4 @@ try { if (!site.config.onVercel) serve(site); -export default site.app; +export default app; diff --git a/src/http/app.js b/src/http/app.js index a0cfc10..2364bb1 100644 --- a/src/http/app.js +++ b/src/http/app.js @@ -48,8 +48,12 @@ function sendVersioned(req, res, etag, body) { return res.json(body); } -export function createApp({ config, catalogue, reporter, logger, assets }) { - const app = express(); +/** + * Adds the site's middleware and routes to an Express app. The app is passed in + * (rather than created here) because Vercel's framework detection only + * recognises an entry file that itself imports and creates Express. + */ +export function createApp({ config, catalogue, reporter, logger, assets, app = express() }) { app.disable('x-powered-by'); app.set('trust proxy', config.trustProxy); app.set('view engine', 'ejs'); diff --git a/src/site.js b/src/site.js index b480845..decb948 100644 --- a/src/site.js +++ b/src/site.js @@ -1,4 +1,5 @@ import { fileURLToPath } from 'node:url'; +import express from 'express'; import { waitUntil } from '@vercel/functions'; import { createCatalogue } from './catalogue.js'; import { loadEnvFile, readConfig } from './config.js'; @@ -15,11 +16,11 @@ const SHUTDOWN_TIMEOUT_MS = 10_000; /** * Builds the whole site: configuration, logging, error reporting, TIDAL and - * the Express app. Starts loading playlists, but does not listen on a port, + * the Express app (created by the caller, see app.js). Starts loading playlists, but does not listen on a port, * so the same app runs as a Vercel Function or as a normal server. * Throws a ConfigError when settings are missing or invalid. */ -export async function createSite() { +export async function createSite({ app = express() } = {}) { loadEnvFile(); const config = readConfig(); @@ -58,7 +59,7 @@ export async function createSite() { versioned: config.isProduction, }); - const app = createApp({ config, catalogue, reporter, logger, assets }); + createApp({ app, config, catalogue, reporter, logger, assets }); await catalogue.start(); return { app, config, logger, reporter, assets }; } From a47b7f9ee8dcea0cc0f287ba0fe645e893b901f7 Mon Sep 17 00:00:00 2001 From: Barry Prendergast Date: Sun, 25 Oct 2026 17:41:54 +0100 Subject: [PATCH 2/2] Register EJS explicitly so Vercel includes it in the function The first working Vercel preview served the API but returned 500 for the page: "Cannot find module 'ejs'". Express loads its view engine by name at runtime, which Vercel's file tracer can't follow, so EJS was left out of the deployed function. Importing it and registering it with app.engine() makes the dependency visible. Co-Authored-By: Claude Sonnet 5.5 --- src/http/app.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/http/app.js b/src/http/app.js index 2364bb1..5478e86 100644 --- a/src/http/app.js +++ b/src/http/app.js @@ -1,6 +1,7 @@ import { randomBytes, randomUUID } from 'node:crypto'; import { fileURLToPath } from 'node:url'; import compression from 'compression'; +import ejs from 'ejs'; import express from 'express'; import helmet from 'helmet'; import { pinoHttp } from 'pino-http'; @@ -56,6 +57,9 @@ function sendVersioned(req, res, etag, body) { export function createApp({ config, catalogue, reporter, logger, assets, app = express() }) { app.disable('x-powered-by'); app.set('trust proxy', config.trustProxy); + // Registered explicitly: Express would load EJS by name at runtime, which Vercel's + // file tracer can't see, so EJS would be missing from the deployed function. + app.engine('ejs', ejs.__express); app.set('view engine', 'ejs'); app.set('views', viewsDir);