Install the Hono CLI
mainInstall the Hono CLI globally using npm to gain access to the hono command for documentation access, server management, and application testing.
npm install -g @hono/clirepository·main·Indexed 43 days ago
https://github.com/honojs/cliA specialized toolset for developing, testing, and optimizing Hono applications. It provides commands to access documentation via `docs` and `search`, test routes without a full server using `request`, start a server with `serve`, and reduce bundle size through aggressive tree-shaking and API removal with `optimize`.
Install the Hono CLI globally using npm to gain access to the hono command for documentation access, server management, and application testing.
npm install -g @hono/cliThe hono command provides a suite of tools for interacting with Hono applications, including documentation access, code optimization, request testing, and server management.
Available commands include:
docs: Access documentation summaries.optimize: Generate optimized Hono applications.search: Search through documentation.request: Send requests to a running Hono app.serve: Start a Hono server.You can check the installed version using the -v or --version flag.
hono --versionThe optimize command uses esbuild with custom plugins to perform tree-shaking at a more aggressive level than standard bundlers. It works through several stages:
GET, HEAD, or OPTIONS routes, the command removes request body parsing methods (e.g., parseBody, json, formData) from the HonoRequest class.mount or route) and removes them from HonoBase.Context object (like html or redirect) and removes them from the Context class.PreparedRegExpRouter from hono/router/reg-exp-router if available in the environment to optimize route matching based on the serialized route parameters found during analysis.esbuild pass with the modified Hono source code to produce the optimized, bundled output.By default, the request command prints the response body to stdout. You can modify this behavior using the following flags:
--json: If the response content-type is JSON, this flag will format the output as a JSON object containing the status, body, and headers.--include: Includes the HTTP status and headers in the output before the body.--head: Shows only the HTTP status and headers, omitting the body.# View only headers
hono request src/index.ts -I
# View headers and body together
hono request src/index.ts -i
# Get a structured JSON object of the whole response
hono request src/index.ts -JYou can capture the output of a request and save it directly to a file using the --output or --remote-name flags.
-o, --output <file> to specify a specific filename for the saved response.-O, --remote-name to automatically name the file based on the request path and the response's content-type.hono request src/index.ts -P /api/data -o response.jsonThe Hono CLI uses tsdown for its build process. You can configure the build behavior using defineConfig from the tsdown package. Key configuration options include:
entry: An array of entry point files.format: The output module format (e.g., ['esm']).dts: Boolean to enable/disable declaration file generation.sourcemap: Boolean to enable/disable sourcemaps.clean: Boolean to clean the output directory before building.minify: Boolean to enable/disable minification.banner: An object containing strings to prepend to the output files (e.g., js for the shebang line #!/usr/bin/env node).target: Specifies the compilation target.outExtensions: A function that receives the format and returns an object mapping extensions to their desired output suffixes.inlineOnly: An array of package names to be inlined into the output bundle.import { defineConfig } from 'tsdown'
export default defineConfig({
entry: ['src/cli.ts'],
format: ['esm'],
dts: false,
sourcemap: false,
clean: true,
minify: false,
banner: {
js: '#!/usr/bin/env node',
},
target: false,
outExtensions({ format }) {
return {
js: format === 'es' ? '.js' : '.cjs',
}
},
inlineOnly: ['@babel/parser', '@jridgewell/sourcemap-codec', 'magic-string'],
})Options for the hono optimize command to control the bundling and optimization process.
-o, --outfile <outfile> Output file
-m, --minify minify output file
-t, --target [target] environment target
--no-request-body-api-removal Disable request body API removal optimization
--no-context-response-api-removal Disable response utility API removal from Context object
--no-hono-api-removal Disable Hono API removal optimizationOptions for the hono serve command to configure the running server.
-p, --port <port> Port number (default: 7070)
--show-routes Show registered routes
--use <middleware> Use middleware (can be used multiple times)
-e, --external <package> Mark package as external (can be used multiple times)Options for the hono request command to control the HTTP request sent to the application.
-P, --path <path> Request path (default: "/")
-X, --method <method> HTTP method (default: GET)
-d, --data <data> Request body data
-H, --header <header> Custom headers (can be used multiple times)
-w, --watch Watch for changes and resend request
-J, --json Output response as JSON
-o, --output <file> Write to file instead of stdout
-O, --remote-name Write output to file named as remote file
-i, --include Include protocol and headers in the output
-I, --head Show only protocol and headers in the output
-e, --external <package> Mark package as external (can be used multiple times)The hono serve command starts a specialized server for Hono applications with built-in TypeScript and JSX support. You can specify a port, show registered routes, and inject middleware directly via the CLI.
# Start server with default empty app
hono serve
# Start server on specific port
hono serve -p 3000 src/app.ts
# Start server and show routes
hono serve --show-routes src/app.ts
# Start server with middleware
hono serve --use 'cors()' src/app.ts
# Start server with multiple middleware
hono serve --use 'cors()' --use 'logger()' src/app.ts
# Start server with external packages
hono serve -e pg -e prisma src/app.tsThe hono optimize command generates an optimized Hono class and exports a bundled file. It applies several tree-shaking optimizations to reduce bundle size:
c.req.json(), c.req.formData(), etc., if the app only uses GET, HEAD, or OPTIONS.c.body(), c.json(), c.text(), etc.route, mount, and fire.You can disable these optimizations using specific flags.
# Generate an optimized Hono class and export bundled file to dist/index.js
hono optimize
# Specify entry file and output file
hono optimize -o dist/app.js src/app.ts
# Export bundled file with minification
hono optimize -m
# Disable specific optimizations
hono optimize -m --no-request-body-api-removalThe hono request command sends HTTP requests to your Hono application using the built-in app.request() method. This is ideal for testing routes without starting a full server. It supports custom paths, methods, data, and headers.
# GET request to default app root (uses src/index.ts or src/index.tsx)
hono request
# GET request to specific path
hono request -P /users/123
# POST request with data
hono request -P /api/users -X POST -d '{"name":"Alice"}'
# Request with custom headers
hono request -P /api/protected \
-H 'Authorization: Bearer token' \
-H 'User-Agent: MyApp' \
src/your-app.ts
# Request with external packages (useful for Node.js native modules)
hono request -e pg -e dotenv src/your-app.ts