# Configuration

kafka.config files, profiles, and resolution order

Package: @cookiemonsterdev/kafka-cli 1.12.1
Canonical: https://cookiemonsterdev.github.io/kafka/docs/cli/guides/configuration/
Source revision: b112a684d2b3cdc7deadb3513041fcdbdc1c18b5

`kafka init` scaffolds a `kafka.config.ts` (or `.mjs`, with `--js`) file in the current directory —
`.ts` when a `tsconfig.json` or a `typescript` dependency is detected, `.mjs` otherwise:

```sh
kafka init
kafka init --force   # overwrite an existing file
```

```ts
export default {
  client: {
    brokers: ['localhost:9092'],
  },
};
```

Every connecting command resolves its options from, highest precedence first: **the command's own
flags → environment variables → the active `--profile` → the config file's `client` section →
built-in defaults**. The config file is discovered by walking upward from the current directory
looking for `kafka.config.{ts,mts,cts,js,mjs,cjs,json}` (or `.config/kafka.*`), stopping at the
first `.git`, `pnpm-workspace.yaml`, or workspace `package.json`. It never searches a
world-writable directory (such as `/tmp`) or one owned by another user (other than root) — not
even the current directory — so running as root over a checkout owned by another uid (common in
CI containers) finds no config. Override that with `--config-file <path>` or `KAFKA_CONFIG`.

The file is loaded asynchronously, so a config that uses top-level `await` or exports an async
factory works. There is no TypeScript transform rescue: an extensionless relative import, or
`export default` in a `.ts` file that resolves to CommonJS, fails with an error naming the fix.

## Profiles

Named alternates — e.g. one entry per cluster — go under a `cli.profiles` section and are selected
with `--profile <name>` or `KAFKA_PROFILE`:

```ts
export default {
  cli: {
    profiles: {
      staging: { brokers: ['staging-1:9092', 'staging-2:9092'] },
      production: {
        brokers: ['prod-1:9092', 'prod-2:9092'],
        sasl: { mechanism: 'plain', username: '...', password: '...' },
      },
    },
  },
};
```

```sh
kafka --profile staging topic list
kafka profiles          # lists every configured profile, marking the active one
```

## Diagnosing what got resolved

`kafka doctor` reports the config file path (if any), the active profile, whether the connection
settings resolve locally, and — when `Kafka`'s own `configSource()` is available — where each value came
from:

```sh
kafka doctor
kafka doctor --profile staging
```

A config file that fails to load, or an unknown `--profile`, doesn't stop `doctor`: it reports the
error on a `config error` line instead. `init`, `completion`, and shell completion never read the
config file at all, so they keep working while it is broken.

Reach for it first whenever a command connects to the wrong cluster, or fails to connect at all.

Next: [Output and scripting with JSON](https://cookiemonsterdev.github.io/kafka/docs/cli/guides/output-and-scripting/).
