honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
20.55k stars 597 forks source link

Deploying to Vercel Serverless Functions #1256

Open PodTT opened 1 year ago

PodTT commented 1 year ago

Is there any official way of deploying a hono app to Vercel serverless functions? I am not talking about edge. I am using prisma with Hono and it would be nice to deploy to serverless functions. As far as I know Prisma is not supported on edge runtimes, it seems like Vercel serverless functions are my only option. I could not find documentation on the website relating to this topic.

yusukebe commented 1 year ago

Hi @PodTT

Have you tried it?

https://hono.dev/getting-started/vercel#node-js

natemate90 commented 1 year ago

@yusukebe Trying this out as well. The app.get example provided is working. However, I get a server 500 error with any different method that I'm trying, i.e. app.post()

sjc5 commented 1 year ago

I'm seeing this as well when using c.req.formData() or c.req.parseBody() (which uses the formData method under the hood).

I suspect it may be related to this: https://github.com/orgs/vercel/discussions/1426

sjc5 commented 1 year ago

Update, it turns out you need to add NODEJS_HELPERS=0 to your environment variables, then Hono will work in Vercel serverless (AWS Lambda)

See https://vercel.com/docs/functions/serverless-functions/runtimes/node-js#disabling-helpers-for-node.js

natemate90 commented 1 year ago

I'm still having trouble. I'll get a 500 response on any post method when adding a JSON payload (locally). It would be nice to see a complete working example somewhere

engageintellect commented 1 year ago

I couldn't get any of the examples in the docs working in vercel. Only locally. Guessing they haven't been updated.

sandys commented 1 year ago

hi folks, we are interested in this as well. is this bug going to be fixed ?

also - we deploy using vercel deploy --prebuilt for all our code (which means we build to vercel build output locally). I'm migrating from hattip which builds and outputs Build Output compatible .vercel directories. Not sure if that is something Hono supports

yusukebe commented 1 year ago

I would like to investigate but I am busy cleaning up other Issues, I think it may be a Vercel issue but I am not sure. I will investigate when I get some time.

sjc5 commented 1 year ago

I'm still having trouble. I'll get a 500 response on any post method when adding a JSON payload (locally). It would be nice to see a complete working example somewhere

This is an Hono server deployed to Vercel: https://github.com/hwy-js/hwy/tree/0f9d7f662984267e24e15a22c960425bcbdb92e2/docs

This does not use the Vercel CLI, so maybe that's where the issues are cropping up for others. Anyway, maybe this example can help someone. There aren't POSTs in this example, but works fine if you add them (both locally and when deployed).

The tricks are:

  1. Set NODEJS_HELPERS=0 in your Vercel production environment variables. POST requests break if you don't do this!
  2. Your function needs to be in an api folder, e.g., ~/api/main.js.
  3. Adjust vercel.json to point to your server entry file.
sandys commented 1 year ago

Hi , Could you elaborate on your deployment please - do u use "vercel build && vercel deploy --prebuilt" for the deployment. I checked the package.json, but couldn't figure how the deployment to vercel was happening.

Also could u clarify the role of api/main js for a full blown hono application? Would that file just contain the router? I'm just trying to figure out a realistic application with many endpoints, etc.

On Thu, Sep 28, 2023, 04:06 Sam Cook @.***> wrote:

I'm still having trouble. I'll get a 500 response on any post method when adding a JSON payload (locally). It would be nice to see a complete working example somewhere

This is an Hono server deployed to Vercel: https://github.com/hwy-js/hwy/tree/0f9d7f662984267e24e15a22c960425bcbdb92e2/docs

This does not use the Vercel CLI, so maybe that's where the issues are cropping up for others. Anyway, maybe this example can help someone. There aren't POSTs in this example, but works fine if you add them (both locally and when deployed).

The tricks are:

  1. Set NODEJS_HELPERS=0 in your Vercel production environment variables. POST requests break if you don't do this!
  2. Your function needs to be in an API folder, e.g., ~/API/main.js.
  3. Adjust vercel.json to point to your server entry file.

— Reply to this email directly, view it on GitHub https://github.com/honojs/hono/issues/1256#issuecomment-1738197919, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAASYUZNTM6C2DMSYCDVFETX4SS7VANCNFSM6AAAAAA2UCI5PE . You are receiving this because you commented.Message ID: @.***>

pkorac commented 3 months ago

@PodTT @sandys and @yusukebe I found a way to get this to work. I had the same problem as you @PodTT – I wanted to run hono api as serveless nodejs function, not on edge. Why? Because I want to use drizzle + postgres … which don't run on edge. I found there are 4 steps:

  1. Install hono using the default instaler and use vercel template
  2. Add the NODEJS_HELPERS=0 to the .env file (and env variables on Vercel)
  3. Add "type": "module" to the package.json
  4. Remo the export const config… from index.ts`

Some further explanation (my understanding)


Github repo with example https://github.com/pkorac/serverlesshono

Hope this helps

scottricity commented 2 weeks ago

@PodTT @sandys and @yusukebe I found a way to get this to work. I had the same problem as you @PodTT – I wanted to run hono api as serveless nodejs function, not on edge. Why? Because I want to use drizzle + postgres … which don't run on edge. I found there are 4 steps:

  1. Install hono using the default instaler and use vercel template
  2. Add the NODEJS_HELPERS=0 to the .env file (and env variables on Vercel)
  3. Add "type": "module" to the package.json
  4. Remo the export const config… from index.ts`

Some further explanation (my understanding)

  • NODEJS helpers I believe let hono do hono instead of vercel's request helpers, etc.
  • Package.json type:module let's you use import something from "something" syntax instead of require()
  • export const config tells vercel to run stuff on edge… which you don't want

Github repo with example https://github.com/pkorac/serverlesshono

Hope this helps

This seems to be the most common and stable fix, I believe we should close this thread with this answer.