React Router Hono Server Integration
react-router-hono-server is a JavaScript/TypeScript library designed to integrate React Router v7 applications with Hono servers, primarily through a Vite plugin. It simplifies the setup of server-side rendering (SSR) and middleware for React Router, enabling development with Vite's Hot Module Replacement (HMR). The library is currently at version 2.25.3, with a release cadence that frequently updates to align with new versions of Hono and React Router. Its key differentiators include native support for React Router v7's unstable middleware API, broad deployment target compatibility (Node, Bun, Cloudflare Workers, AWS Lambda), and an opinionated but customizable default Hono server configuration. It strictly operates in ESM mode and leverages Vite for its build and development pipeline. The package relies on peer dependencies like `hono`, `react-router`, and `@hono/node-server` to function.
Common errors
-
Module 'react-router' has no exported member 'Future'.
cause The TypeScript declaration for `Future` with `v8_middleware` flag is missing or incorrectly placed.fixAdd the type declaration `declare module 'react-router' { interface Future { v8_middleware: true; } }` to a global TypeScript declaration file in your project. -
SyntaxError: require() of ES Module ... not supported. Instead change the require to a dynamic import() or top-level import statement.
cause Attempting to use CommonJS `require()` to import modules from `react-router-hono-server`, which is an ESM-only package.fixRefactor all module imports to use ES module `import ... from '...'` syntax. Ensure your Node.js environment is configured to run ESM. -
Error: [plugin:react-router-hono-server] Server entry point not found. Please provide one or use the default 'server.ts' or 'server/index.ts'.
cause The Vite plugin could not locate a server entry file as per its default conventions or a specified `serverEntryPoint` option.fixCreate a `server.ts` file in your application's root directory, or specify the correct path to your server entry file using the `serverEntryPoint` option in `reactRouterHonoServer({ serverEntryPoint: './src/my-server-entry.ts' })` in `vite.config.ts`. -
Error: Cannot find module 'hono' or Error: 'hono' is not found in the root dependency tree.
cause The `hono` peer dependency is not correctly installed or linked, often due to package manager strictness (e.g., pnpm).fixEnsure `hono` is explicitly installed in your project (`npm install hono` or `yarn add hono`). If using pnpm, add `public-hoist-pattern[]=hono` to your `.npmrc` file.
Warnings
- breaking This package is strictly compatible only with React Router v7. It is not compatible with previous versions (e.g., Remix or older React Router versions).
- breaking The library is ESM-only. CommonJS (CJS) `require()` statements are not supported for importing any modules from this package.
- gotcha For correct TypeScript typings, especially when using React Router's middleware, you must declare `v8_middleware: true` in the `Future` interface of the `react-router` module.
- gotcha The `hono` package is a peer dependency. If using pnpm, you might encounter issues with `hono` not being found due to pnpm's strict dependency linking.
- gotcha The `react-router-hono-server/dev` Vite plugin is required for development, HMR, and to trigger the server build process.
- gotcha By default, the plugin looks for a server entry point at `${appDirectory}/server.ts` or in a folder named `${appDirectory}/server`. These paths are reserved unless a custom `serverEntryPoint` is explicitly configured in the plugin options.
Install
-
npm install react-router-hono-server -
yarn add react-router-hono-server -
pnpm add react-router-hono-server
Imports
- reactRouterHonoServer
const reactRouterHonoServer = require('react-router-hono-server/dev');import { reactRouterHonoServer } from 'react-router-hono-server/dev'; - createHonoServer
import { createHonoServer } from 'react-router-hono-server';import { createHonoServer } from 'react-router-hono-server/node'; - Future
declare module 'react-router' { interface Future { v8_middleware: true; } }
Quickstart
import { reactRouter } from "@react-router/dev/vite";
import { reactRouterHonoServer } from "react-router-hono-server/dev";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
export default defineConfig({
plugins: [
reactRouterHonoServer(), // Add this plugin to enable Hono server integration
reactRouter(), // Required React Router Dev plugin
tsconfigPaths()
],
// Optional: Configure build outputs for server (e.g., for Cloudflare Workers)
// build: {
// rollupOptions: {
// output: {
// entryFileNames: '[name].js',
// chunkFileNames: '[name]-[hash].js',
// assetFileNames: '[name].[ext]'
// }
// }
// }
});
// This minimal setup automatically generates a default Hono server.
// For custom server logic, create a 'server.ts' file at your app's root
// or configure the 'serverEntryPoint' option in reactRouterHonoServer().