Configure Vercel to treat a compiled index.js as a serverless function

I’m working on a monorepo with the following setup:

  • index.ts (using the Hono framework with hono/vercel) is compiled using tsup into dist/functions/index.js
  • Static assets are built with Vite into dist/vite/assets

So my build structure ends up like this:

dist/
├── functions/
│   └── index.js ← this is my API handler
└── vite/
    └── assets/

My vercel.json looks like this:

{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "version": 2,
  "installCommand": "pnpm install",
  "buildCommand": "pnpm turbo run build",
  "outputDirectory": "dist",
  "rewrites": [
    {
      "source": "/assets/(.*)",
      "destination": "/dist/vite/assets/$1"
    },
    {
      "source": "/(.*)",
      "destination": "/dist/functions/index.js"
    }
  ]
}

Even though I set up a rewrite to /functions/index.js, Vercel is treating the file as a static asset instead of a serverless function. The file is publicly available (e.g. site.vercel.app/functions/index.js) but it’s not executed as a function.

I tried changing the tsup output to api/index.js instead (since Vercel treats files inside the /api directory as functions). But that didn’t work, the api/index.js file didn’t show up in the Vercel deployment output. I guess that Vercel doesn’t allow emitting files outside of the declared “outputDirectory” (which is dist), so writing to ../api is being ignored.

Question:

Is there a supported way to include a compiled serverless function in the output directory and still have Vercel treat it as a serverless function?

Or, is there a way to output a file to /api at the root level (outside outputDirectory) so Vercel recognizes it?

1 Like

Hi @davi-ldc, welcome to the Vercel Community!

So, it seems like the way your build outputs are generated they are treated as static assets.

Have you tried using the Vercel - Hono adapter?

If you need more help, please share your public repo or a minimal reproducible example. That will let us all work together from the same code to figure out what’s going wrong.

Hey Anshuman, thanks for the reply!

Sorry for the lack of information. I made a minimal example repo to reproduce the issue:

If you try deploying it to Vercel and select the app/ folder as root, all the output files (including dist/functions/index.js that should be a serverless function) get treated as static assets instead.

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