Skip to content

fix(api): Vercel serverless handler + common CJS dual-build#32

Open
kuyacarlo wants to merge 7 commits into
feat/api-db-statsfrom
fix/api-vercel-esm
Open

fix(api): Vercel serverless handler + common CJS dual-build#32
kuyacarlo wants to merge 7 commits into
feat/api-db-statsfrom
fix/api-vercel-esm

Conversation

@kuyacarlo

Copy link
Copy Markdown
Contributor

Stacked on #31. Fixes the live production crash.

Summary

  • Root cause: ERR_REQUIRE_ESM — Nest CJS require('@worksight/common') against an ESM-only package. Every request → FUNCTION_INVOCATION_FAILED.
  • @worksight/common now dual-builds dist/esm + dist/cjs with correct exports.import / exports.require.
  • Real serverless entry: api/index.jsdist/vercel.js (Express adapter, warm-cache). Dropped outputDirectory: dist (that made Vercel treat every compiled file as a function). vercel.json rewrites /(.*)/api.
  • Re-added DATABASE_URL / DATABASE_URL_DIRECT / CORS_ORIGINS to the recreated worksight-api project.

Test plan

  • require('@worksight/common') and ESM import both load
  • pnpm --filter @worksight/api test — 19 green; web type-check green
  • Local serverless shim: GET /health → 200 database: postgres
  • vercel --prod deploy returns 200 on /health and /users

Made with Cursor

Production crashed on every request with ERR_REQUIRE_ESM: Nest emits
CommonJS but @worksight/common was ESM-only, so require() of the
workspace package killed the isolate. Dual-build common to dist/esm +
dist/cjs and wire package exports accordingly.

Also replace the broken "outputDirectory: dist" deploy (Vercel treated
compiled files as functions, and main.ts called app.listen()) with a
real serverless entry: api/index.js → dist/vercel.js over an Express
adapter, rewrite /(.*) → /api. Local boot still uses main.ts + listen.

Co-authored-by: Cursor <cursoragent@cursor.com>
@vercel

vercel Bot commented Jul 25, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
worksight-api Ready Ready Preview, Comment Jul 26, 2026 1:43am
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
worksight-docs Skipped Skipped Jul 26, 2026 1:43am
worksight-web Skipped Skipped Jul 26, 2026 1:43am

Co-authored-by: Cursor <cursoragent@cursor.com>
@vercel
vercel Bot temporarily deployed to Preview – worksight-web July 25, 2026 22:53 Inactive
@vercel
vercel Bot temporarily deployed to Preview – worksight-docs July 25, 2026 22:53 Inactive
Co-authored-by: Cursor <cursoragent@cursor.com>
Nest Swagger UI returned HTML but its local swagger-ui-dist CSS/JS never
shipped in the serverless bundle (404). Stop mounting that UI. Expose
OpenAPI at /openapi.json (and /api-json), put Scalar at /api and
/reference, and keep a CDN-backed Swagger UI at /swagger.

Co-authored-by: Cursor <cursoragent@cursor.com>
@scalar/nestjs-api-reference pulls ESM-only client-side-rendering and
crashes the CJS Nest serverless bundle with ERR_REQUIRE_ESM on every
request. Drop that dependency; serve Scalar and Swagger UI as small HTML
shells that load their UIs from CDN against /openapi.json.

Co-authored-by: Cursor <cursoragent@cursor.com>
The data-url + standalone.min.js shell rendered a blank page. Switch to
Scalar.createApiReference('#app', { url }) per current html-js docs.

Co-authored-by: Cursor <cursoragent@cursor.com>
Remove the root hello handler (404s now). Add Nest Swagger decorators
and DTO schemas for every remaining endpoint so Scalar/Swagger show
request/response shapes, optional query params, and error responses.

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant