> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/oven-sh/bun/llms.txt
> Use this file to discover all available pages before exploring further.

# File Types

> File types and built-in loaders supported by Bun's runtime and bundler.

Bun uses the file extension to select a built-in *loader* that parses and transforms each file. As a rule of thumb, the runtime and bundler support the same set of file types out of the box.

Supported extensions: `.js` `.cjs` `.mjs` `.mts` `.cts` `.ts` `.tsx` `.jsx` `.css` `.json` `.jsonc` `.json5` `.toml` `.yaml` `.yml` `.txt` `.wasm` `.node` `.html` `.sh`

You can explicitly override the loader for any import using the `with { type: "..." }` import attribute:

```typescript theme={null}
import config from "./my_file" with { type: "toml" };

// Dynamic import
const { default: config } = await import("./my_file", { with: { type: "toml" } });
```

## JavaScript and TypeScript

### `js` loader

Default for `.cjs` and `.mjs`. Parses JavaScript and applies default transforms like dead-code elimination and tree shaking. Bun does not down-convert syntax.

### `jsx` loader

Default for `.js` and `.jsx`. Same as `js`, but JSX syntax is supported and transpiled to plain JavaScript. The JSX transform is controlled by `jsx*` fields in `tsconfig.json`.

### `ts` loader

Default for `.ts`, `.mts`, and `.cts`. Strips TypeScript syntax, then behaves identically to the `js` loader. Bun does not perform type checking.

### `tsx` loader

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

<Note>
  All four loaders can be used with `require()` as well as `import`. You can require a `.ts` or `.tsx` file from a CommonJS module.
</Note>

## Data formats

### JSON

Default for `.json`. JSON files can be imported directly:

```typescript theme={null}
import pkg from "./package.json";
pkg.name; // "my-package"
```

During bundling, the parsed JSON is inlined as a JavaScript object:

```typescript theme={null}
var pkg = {
  name: "my-package",
  // ...other fields
};
```

If a `.json` file is passed as a bundler entrypoint, it is converted to a `.js` module that `export default`s the parsed object.

<CodeGroup>
  ```json input.json theme={null}
  {
    "name": "John Doe",
    "age": 35,
    "email": "johndoe@example.com"
  }
  ```

  ```typescript output.js theme={null}
  export default {
    name: "John Doe",
    age: 35,
    email: "johndoe@example.com",
  };
  ```
</CodeGroup>

### JSONC

Default for `.jsonc`. JSONC (JSON with Comments) files can be imported directly. Bun strips comments and trailing commas during parsing.

```typescript theme={null}
import config from "./config.jsonc";
console.log(config);
```

<Note>
  Bun automatically uses the `jsonc` loader for `tsconfig.json`, `jsconfig.json`, `package.json`, and `bun.lock`.
</Note>

### TOML

Default for `.toml`. TOML files can be imported directly using Bun's fast native TOML parser:

```typescript theme={null}
import config from "./bunfig.toml";
config.logLevel; // "debug"
```

During bundling, the parsed TOML is inlined as a JavaScript object:

```typescript theme={null}
var config = {
  logLevel: "debug",
};
```

If a `.toml` file is passed as a bundler entrypoint, it becomes a `.js` module that `export default`s the parsed object.

<CodeGroup>
  ```toml input.toml theme={null}
  name = "John Doe"
  age = 35
  email = "johndoe@example.com"
  ```

  ```typescript output.js theme={null}
  export default {
    name: "John Doe",
    age: 35,
    email: "johndoe@example.com",
  };
  ```
</CodeGroup>

### YAML

Default for `.yaml` and `.yml`. YAML files can be imported directly using Bun's native YAML parser:

```typescript theme={null}
import config from "./config.yaml";
console.log(config);
```

You can also use the import attribute to force YAML parsing on any file:

```typescript theme={null}
import data from "./data.txt" with { type: "yaml" };
```

### JSON5

Default for `.json5`. JSON5 is a superset of JSON that supports comments, trailing commas, unquoted keys, and single-quoted strings:

```typescript theme={null}
import config from "./config.json5";
console.log(config);
```

<CodeGroup>
  ```json5 input.json5 theme={null}
  {
    // Configuration
    name: "John Doe",
    age: 35,
    email: "johndoe@example.com",
  }
  ```

  ```typescript output.js theme={null}
  export default {
    name: "John Doe",
    age: 35,
    email: "johndoe@example.com",
  };
  ```
</CodeGroup>

## Text and static files

### `text` loader

Default for `.txt`. The file contents are imported as a string:

```typescript theme={null}
import contents from "./file.txt";
console.log(contents); // "Hello, world!"
```

To import an HTML file as raw text (overriding the default HTML loader):

```typescript theme={null}
import html from "./index.html" with { type: "text" };
```

During bundling, the string is inlined:

```typescript theme={null}
var contents = `Hello, world!`;
```

If a `.txt` file is passed as a bundler entrypoint, it becomes a `.js` module that `export default`s the string.

### `file` loader

Default for all unrecognized file types. The import resolves to a path or URL pointing to the file. Commonly used for media and font assets:

```typescript theme={null}
import logo from "./logo.svg";
console.log(logo); // "/path/to/project/logo.svg"
```

In the bundler, the file is copied to `outdir` and the import resolves to a relative path. If `publicPath` is configured, it is used as a prefix:

| Public path                  | Resolved import                    |
| ---------------------------- | ---------------------------------- |
| `""` (default)               | `/logo.svg`                        |
| `"/assets"`                  | `/assets/logo.svg`                 |
| `"https://cdn.example.com/"` | `https://cdn.example.com/logo.svg` |

## Special loaders

### `html` loader

Default for `.html`. Processes HTML files and bundles all referenced assets:

* Bundles and hashes referenced JavaScript (`<script src="...">`)
* Bundles and hashes referenced CSS (`<link rel="stylesheet" href="...">`)
* Hashes referenced images (`<img src="...">`)
* Preserves external URLs (`http://` and `https://`)

<CodeGroup>
  ```html src/index.html theme={null}
  <!DOCTYPE html>
  <html>
    <body>
      <img src="./image.jpg" alt="Local image" />
      <img src="https://example.com/image.jpg" alt="External image" />
      <script type="module" src="./script.js"></script>
    </body>
  </html>
  ```

  ```html dist/output.html theme={null}
  <!DOCTYPE html>
  <html>
    <body>
      <img src="./image-HASHED.jpg" alt="Local image" />
      <img src="https://example.com/image.jpg" alt="External image" />
      <script type="module" src="./output-HASHED.js"></script>
    </body>
  </html>
  ```
</CodeGroup>

<Note>
  The `html` loader behaves differently depending on context:

  * **Static build** (`bun build ./index.html`): produces a static site with bundled and hashed assets.
  * **Runtime** (`bun run server.ts` importing an HTML file): bundles assets on the fly during development with hot module replacement.
  * **Full-stack build** (`bun build --target=bun server.ts`): the import resolves to a manifest object that `Bun.serve` uses to serve pre-bundled assets in production.
</Note>

### `css` loader

Default for `.css`. CSS files can be imported directly. No value is returned from the import — it is used for side effects only, primarily in full-stack applications:

```typescript theme={null}
import "./styles.css";
```

### `napi` loader

Default for `.node`. Native Node.js addons can be imported directly at runtime:

```typescript theme={null}
import addon from "./addon.node";
console.log(addon);
```

In the bundler, `.node` files are handled by the `file` loader.

### `sqlite` loader

Triggered by the `with { type: "sqlite" }` import attribute. Imports an SQLite database directly using `bun:sqlite`:

```typescript theme={null}
import db from "./my.db" with { type: "sqlite" };
```

To embed the database into a standalone executable:

```typescript theme={null}
import db from "./my.db" with { type: "sqlite", embed: "true" };
```

This loader is only supported when `target` is `bun`.

### `sh` loader

Default for `.sh` files. Executes [Bun Shell](/runtime/shell) scripts. Only available at runtime (not in the bundler):

```bash theme={null}
bun run ./script.sh
```
