node-server icon indicating copy to clipboard operation
node-server copied to clipboard

Bug: Unable to use Hono in Nextjs 14 App Dir with nodejs runtime

Open MathurAditya724 opened this issue 1 year ago • 5 comments

Unable to use hono in nextjs app dir with nodejs runtime

Example Code

// app/api/[[...route]]/route.ts
import { Hono } from 'hono'
import { handle } from '@hono/node-server/vercel'

const app = new Hono().basePath('/api')

app.get('/hello', async (c) => {
    return c.json({
        message: 'Hello Next.js!',
    })
})

export const GET = handle(app)

Error - image

This code has been recreated from the docs https://hono.dev/getting-started/vercel#node-js for the app dir. It works if we set the runtime to edge and use the hono/vercel package like this -

import { Hono } from 'hono'
import { handle } from 'hono/vercel'

export const runtime = "edge"

const app = new Hono().basePath('/api')

app.get('/hello', async (c) => {
    return c.json({
        message: 'Hello Next.js!',
    })
})

export const GET = handle(app)

MathurAditya724 avatar Jun 09 '24 18:06 MathurAditya724

I have the same bug trying to run Hono (large application setup) with Next.js (App router) using Node.js runtime

You don't have to specify export const runtime = "edge" for it to work on Vercel tho.

I think the bug comes from the original request response being modified by Next.js

Axibord avatar Jun 09 '24 18:06 Axibord

I can't run on edge for the reason of having to do native connection to Postgres (complained about pg-native needing fs). I was able to use the nodejs runtime with App Router (deploying to Vercel). The difference is I import handle from hono/vercel instead.

firstian avatar Jun 09 '24 19:06 firstian

Interesting, yes it is working. We should update the docs.

MathurAditya724 avatar Jun 09 '24 19:06 MathurAditya724

On another thought, what's the purpose of the @hono/node-server/vercel file? Only for the pages dir?

MathurAditya724 avatar Jun 09 '24 20:06 MathurAditya724

Where is the documentation for this runtime setting? I can see docs to the RuntimeKey, but that looks somewhat different than this. I only found out how this works by setting it to something else and look through the error messages.

firstian avatar Jun 09 '24 21:06 firstian

Where is the documentation for this runtime setting?

@firstian It's here: https://hono.dev/docs/getting-started/vercel#node-js

The hono.dev documentation is kinda vague. It doesn't explicitly explain what each line does.

For example, the constant config: pageConfig is not being used and also it comes from Next.js, which also they dont have any documentation on this. TBH I dont know what this constant is for.

Anyone know how to setup so that I can use node.js runtime in my Next.js 14? (actually 15 since is stable now), I will highly appreciate it.

joseavr avatar Oct 22 '24 04:10 joseavr

Yeah the documentation is confusing.

  1. Is it required to use edge runtime with this import { handle } from 'hono/vercel'?
  • In my local dev, it works without setting runtime to edge
  1. Is this required to run in nodejs runtime? import { handle } from '@hono/node-server/vercel'
  • My app errors if I try to use this
  1. As someone asked above, what scenarios is this needed for? App directory? Pages directory? Edge? Node?
export const config: PageConfig = {
  api: {
    bodyParser: false,
  },
}

Edit: [SOLUTION]

Okay, from my tests, I've come to these conclusions...

  1. import { handle } from 'hono/vercel' works for BOTH node and edge. This handle is for App Directory
  2. import { handle } from '@hono/node-server/vercel' is for Pages Directory
  3. The PageConfig stuff is for Pages Directory. Does not apply to App Directory

Also take note that... in the App Directory, the export is

export const GET = handle(app)
export const POST = handle(app)

Whereas in the Pages Directory, the export is

export default handle(app)

Very easy to miss this difference.

Lastly, as a helpful snippet, you can add this to your route to check the runtime

app.get("/hello", (c) => {
  // Check if running in Node.js
  const isNode =
    typeof process !== "undefined" &&
    process.versions != null &&
    process.versions.node != null;

  // Log the runtime environment
  console.log(`Running in ${isNode ? "Node.js" : "Edge"} environment`);

  return c.json({
    message: "Hello Next.js!",
  });
});

Docs should clarify all of this imo.

paulwongx avatar Oct 24 '24 17:10 paulwongx

Where is the documentation for this runtime setting?

@firstian It's here: https://hono.dev/docs/getting-started/vercel#node-js

The hono.dev documentation is kinda vague. It doesn't explicitly explain what each line does.

For example, the constant config: pageConfig is not being used and also it comes from Next.js, which also they dont have any documentation on this. TBH I dont know what this constant is for.

Anyone know how to setup so that I can use node.js runtime in my Next.js 14? (actually 15 since is stable now), I will highly appreciate it.

See my comment above for clarification on the docs. It should work with import { handle } from 'hono/vercel'

paulwongx avatar Oct 24 '24 18:10 paulwongx

@yusukebe how can we add these details to the docs?

rafaell-lycan avatar Nov 05 '24 17:11 rafaell-lycan

@rafaell-lycan

Is this good for you? https://github.com/honojs/website/pull/528

yusukebe avatar Nov 12 '24 20:11 yusukebe

Yes, this provides much-needed clarity on the issue discussed here.

MathurAditya724 avatar Nov 12 '24 20:11 MathurAditya724

Okay! Shall we close this issue?

yusukebe avatar Nov 12 '24 21:11 yusukebe