Node.js does not work in Next.js
My hono implementation works fine on edge in Next.js. But, I am getting the following error when migrating from edge (hono/vercel) to nodejs (@hono/node-server/vercel).
(Continued in thread)
⨯ TypeError: outgoing.writeHead is not a function
at responseViaResponseObject (webpack-internal:///(rsc)/./node_modules/@hono/node-server/dist/vercel.mjs:373:14)
at eval (webpack-internal:///(rsc)/./node_modules/@hono/node-server/dist/vercel.mjs:416:14)
at node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:53452
at node_modules/next/dist/server/lib/trace/tracer.js:140:36
at NoopContextManager.with (node_modules/@opentelemetry/api/build/src/context/NoopContextManager.js:25:19)
at ContextAPI.with (node_modules/@opentelemetry/api/build/src/api/context.js:60:46)
at NoopTracer.startActiveSpan (node_modules/@opentelemetry/api/build/src/trace/NoopTracer.js:65:31)
at ProxyTracer.startActiveSpan (node_modules/@opentelemetry/api/build/src/trace/ProxyTracer.js:36:24)
at node_modules/next/dist/server/lib/trace/tracer.js:122:103
at NoopContextManager.with (node_modules/@opentelemetry/api/build/src/context/NoopContextManager.js:25:19)
at ContextAPI.with (node_modules/@opentelemetry/api/build/src/api/context.js:60:46)
at NextTracerImpl.trace (node_modules/next/dist/server/lib/trace/tracer.js:122:28)
at node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:46261
at AsyncLocalStorage.run (node:async_hooks:346:14)
at Object.wrap (node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:39188)
at node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:45368
at AsyncLocalStorage.run (node:async_hooks:346:14)
at Object.wrap (node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:37583)
at node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:45330
⨯ TypeError: outgoing.writeHead is not a function
at responseViaResponseObject (webpack-internal:///(rsc)/./node_modules/@hono/node-server/dist/vercel.mjs:373:14)
at eval (webpack-internal:///(rsc)/./node_modules/@hono/node-server/dist/vercel.mjs:416:14)
at node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:53452
at node_modules/next/dist/server/lib/trace/tracer.js:140:36
at NoopContextManager.with (node_modules/@opentelemetry/api/build/src/context/NoopContextManager.js:25:19)
at ContextAPI.with (node_modules/@opentelemetry/api/build/src/api/context.js:60:46)
at NoopTracer.startActiveSpan (node_modules/@opentelemetry/api/build/src/trace/NoopTracer.js:65:31)
at ProxyTracer.startActiveSpan (node_modules/@opentelemetry/api/build/src/trace/ProxyTracer.js:36:24)
at node_modules/next/dist/server/lib/trace/tracer.js:122:103
at NoopContextManager.with (node_modules/@opentelemetry/api/build/src/context/NoopContextManager.js:25:19)
at ContextAPI.with (node_modules/@opentelemetry/api/build/src/api/context.js:60:46)
at NextTracerImpl.trace (node_modules/next/dist/server/lib/trace/tracer.js:122:28)
at node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:46261
at AsyncLocalStorage.run (node:async_hooks:346:14)
at Object.wrap (node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:39188)
at node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:45368
at AsyncLocalStorage.run (node:async_hooks:346:14)
at Object.wrap (node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:37583)
at node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:45330
1 Reply
at AsyncLocalStorage.run (node:async_hooks:346:14)
at e_.execute (node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:44777)
at e_.handle (node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:54711)
at doRender (node_modules/next/dist/server/base-server.js:1377:60)
at cacheEntry.responseCache.get.routeKind (node_modules/next/dist/server/base-server.js:1587:46)
at ResponseCache.get (node_modules/next/dist/server/response-cache/index.js:49:26)
at DevServer.renderToResponseWithComponentsImpl (node_modules/next/dist/server/base-server.js:1507:53)
at AsyncLocalStorage.run (node:async_hooks:346:14)
at e_.execute (node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:44777)
at e_.handle (node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:54711)
at doRender (node_modules/next/dist/server/base-server.js:1377:60)
at cacheEntry.responseCache.get.routeKind (node_modules/next/dist/server/base-server.js:1587:46)
at ResponseCache.get (node_modules/next/dist/server/response-cache/index.js:49:26)
at DevServer.renderToResponseWithComponentsImpl (node_modules/next/dist/server/base-server.js:1507:53)
api/[[...route]]/route.ts
file
import { Hono } from "hono";
import links from "./links";
import auth from "./auth";
import { HTTPException } from "hono/http-exception";
import { handle } from "@hono/node-server/vercel";
import type { PageConfig } from "next";
export const config: PageConfig = {
api: {
bodyParser: false,
},
};
export const runtime = "nodejs";
const app = new Hono().basePath("/api");
app.onError((err, c) => {
if (err instanceof HTTPException) {
return err.getResponse();
}
return c.json({ message: "Internal Error" }, 500);
});
const routes = app
.route("/links", links)
.route("/auth", auth)
export const GET = handle(app);
export const POST = handle(app);
export type AppType = typeof routes;
import { Hono } from "hono";
import links from "./links";
import auth from "./auth";
import { HTTPException } from "hono/http-exception";
import { handle } from "@hono/node-server/vercel";
import type { PageConfig } from "next";
export const config: PageConfig = {
api: {
bodyParser: false,
},
};
export const runtime = "nodejs";
const app = new Hono().basePath("/api");
app.onError((err, c) => {
if (err instanceof HTTPException) {
return err.getResponse();
}
return c.json({ message: "Internal Error" }, 500);
});
const routes = app
.route("/links", links)
.route("/auth", auth)
export const GET = handle(app);
export const POST = handle(app);
export type AppType = typeof routes;