When run with Node.js, a version of 22 (LTS) or higher is required. Please
verify your version by running node -v, and
upgrade if necessary.
Now create a bknd.config.ts file in the root of your project.
bknd.config.ts
import type { ViteBkndConfig } from "bknd/adapter/vite";export default { connection: { url: "file:data.db", },} satisfies ViteBkndConfig;
See bknd.config.ts for more information on how to configure bknd.
The ViteBkndConfig type extends the BkndConfig type with the following additional properties:
export type ViteEnv = NodeJS.ProcessEnv;export type ViteBkndConfig<Env = ViteEnv> = RuntimeBkndConfig<Env> & {};
To serve the bknd API, you first have to create a local server file for you vite environment.
Create a server.ts file:
server.ts
import { serve } from "bknd/adapter/vite";import config from "./bknd.config";export default serve(config);
You can also run your vite server in mode: "fresh", this will re-create the app on every fetch.
This is only useful for when working on the bknd repository directly.
For more information about the connection object, refer to the Database guide.
Next, adjust your vite.config.ts to look like the following:
vite.config.ts
import { devServer } from "bknd/adapter/vite";import react from "@vitejs/plugin-react";import { defineConfig } from "vite";import tsconfigPaths from "vite-tsconfig-paths";// https://vitejs.dev/config/export default defineConfig({ plugins: [ react(), tsconfigPaths(), devServer({ // point to your previously created server file entry: "./server.ts", }), ],});
This is just the bare minimum and may not always fulfill your requirements. There are a few
options you can make use of to adjust it according to your setup.
There might be cases you want to be sure to be in control over the HTML that is being used.
bknd generates it automatically, but you use your own one as follows:
server.ts
import { serve, addViteScript } from "bknd/adapter/vite";import { readFile } from "node:fs/promises";import config from "./bknd.config";let html = await readFile("./index.html", "utf-8");// then add it as an optionexport default serve({ ...config, adminOptions: { html: addViteScript(html), // optionally, you can change the base path for the admin UI adminBasePath: "/admin", },});
The vite scripts has to be added manually currently, as adding them automatically with
@hono/vite-dev-server is buggy. This may change in the future.
By default, the entry point /src/main.tsx is used and should fit most cases. If that's not you,
you can supply a different one like so:
server.ts
import { serve } from "bknd/adapter/vite";import config from "./bknd.config";// the configuration given is optionalexport default serve({ ...config, adminOptions: { forceDev: { mainPath: "/src/special.tsx", }, },});