Skip to main content
Bun uses the file extension to determine which built-in loader to apply. The same set of loaders is used by both the runtime and the bundler. Supported extensions out of the box: .js .jsx .cjs .mjs .mts .cts .ts .tsx .css .json .jsonc .toml .yaml .yml .txt .wasm .node .html

Importing non-JS files

You can explicitly specify a loader using the type import attribute:

Built-in loaders

js — JavaScript

Default for .cjs and .mjs. Parses the file and applies dead-code elimination and tree shaking. Bun does not down-convert syntax.

jsx — JavaScript + JSX

Default for .js and .jsx. Same as js, but JSX syntax is also supported. JSX is transformed to plain JavaScript based on your tsconfig.json compilerOptions.jsx setting.

ts — TypeScript

Default for .ts, .mts, .cts. Strips all TypeScript syntax, then behaves like the js loader. No type checking is performed.

tsx — TypeScript + JSX

Default for .tsx. Transpiles both TypeScript and JSX to vanilla JavaScript.

json — JSON

Default for .json. JSON files can be imported directly and are inlined as JavaScript objects in the bundle.
When a .json file is used as a bundler entrypoint, it is converted to a .js module that export defaults the parsed object.

jsonc — JSON with Comments

Default for .jsonc. Strips comments and trailing commas, then behaves like the json loader. Bun automatically uses this loader for tsconfig.json, jsconfig.json, package.json, and bun.lock.

toml — TOML

Default for .toml. Parses the file using Bun’s native TOML parser and inlines the result as a JavaScript object.

yaml — YAML

Default for .yaml and .yml. Parses the file using Bun’s native YAML parser and inlines the result as a JavaScript object.

text — Plain text

Default for .txt. File contents are read and inlined as a string.
Override the loader to import any file as text:

css — CSS

Default for .css. CSS files can be imported from JavaScript. The bundler processes @import statements and url() references, and all imported CSS is combined into a single .css file in outdir.
See CSS Bundling for full details including CSS Modules and syntax lowering.

html — HTML

Default for .html. Processes an HTML file and bundles any referenced assets.
  • <script src="..."> — bundled and hashed
  • <link rel="stylesheet" href="..."> — bundled and hashed
  • <img src="..."> — hashed
  • External URLs (http://, https://) — preserved as-is
The html loader behaves differently depending on context. With bun build ./index.html it produces a static site. When imported from server code and used with Bun.serve(), it enables the fullstack dev server with HMR. See Fullstack Development for more.

file — File (assets)

Default for all unrecognized extensions. The file is copied to outdir as-is and the import is replaced with the relative path to the copied file.
With a publicPath configured:
The copied file name is determined by naming.asset (default: [name]-[hash].[ext]).

sqlite — SQLite database

Requires with { type: "sqlite" }. Loads the database using bun:sqlite.
To embed the database into a standalone executable, add embed: "true":
SQLite imports require --target=bun.

napi — Native addon

Default for .node. Native addons can be imported at runtime:
In the bundler, .node files are treated as assets (copied using the file loader).

Custom loaders in Bun.build()

Map additional file extensions to built-in loaders using the loader option:

Loader name reference

Custom loaders via plugins

For file types not covered by built-in loaders, use the plugin API:
See Bundler Plugins for full documentation.