This Bun plugin allows for support of auto-imports in your server-side code.
- Auto-imports support for Bun
bun install -d bun-plugin-auto-importsYou may now use the plugin:
// index.ts
import type { AutoImportsOptions } from 'bun-plugin-auto-imports'
import { plugin } from 'bun'
import { autoImports } from 'bun-plugin-auto-imports'
const options: AutoImportsOptions = {
presets: ['solid-js'], // any unimport presets are valid
imports: [{ name: 'z', from: 'zod' }],
dirs: ['./src'],
dts: `./src/auto-import.d.ts`, // default is `./auto-import.d.ts`
}
plugin(autoImports(options))
Bun.serve({
fetch: handler,
port: 3000,
})In your "server file," you may now use the auto-imported modules:
// server.ts
// `z` is auto imported from zod
const Body = z.object({
msg: z.string(),
})
export async function handler(req: Request) {
try {
const body = await req.json()
const data = Body.parse(body)
return new Response(`Received: ${data.msg}`)
}
catch (e) {
return new Response('Invalid body', { status: 400 })
}
}Note
If you are familiar with unimport, AutoImportsOptions proxies UnimportOptions.
Use moduleSpecifier(filePath, importerPath) when scanned files belong to a
published package. Return its public package specifier, including the source
extension when required by its exports. Return undefined for ordinary local
files to keep relative imports.
import { relative, sep } from 'node:path'
import { generateRuntimeIndex } from 'bun-plugin-auto-imports'
await generateRuntimeIndex([defaultsDir], outputPath, {
moduleSpecifier(filePath) {
const suffix = relative(defaultsDir, filePath)
if (suffix.startsWith(`..${sep}`) || suffix === '..') return undefined
return `@example/defaults/${suffix.split(sep).join('/')}`
},
})The same option is available on autoImports() for runtime imports and generated
declarations, and as the fourth argument to generateGlobalsScript(). This keeps
generated package imports stable when the consumer directory or install layout
changes. The package must export every returned specifier.
Directory scans use the caller's directory order and sorted file paths. The first registration of an exported name wins, matching auto-import injection. Runtime indexes, their returned export metadata, globals scripts and global declarations use that same selection. Repeated overload signatures and re-exports do not emit duplicate names. Import a module explicitly when helpers in different modules share a name but have different meanings.
bun testPlease see our releases page for more information on what has changed recently.
Please review the Contributing Guide for details.
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using this package:
Join the Stacks Discord Server
“Software that is free, but hopes for a postcard.” We love receiving postcards from around the world showing where bun-plugin-auto-imports is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
Many thanks to the following core technologies & people who have contributed to this package:
The MIT License (MIT). Please see LICENSE for more information.
Made with 💙

