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

# 设置环境变量

当前环境变量可通过 `process.env` 或 `Bun.env` 访问。

```ts index.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
Bun.env.API_TOKEN; // => "secret"
process.env.API_TOKEN; // => "secret"
```

***

在 `.env` 文件中设置这些变量。

Bun 会自动读取以下文件（按优先级递增顺序列出）。

* `.env`
* `.env.production`、`.env.development`、`.env.test`（取决于 `NODE_ENV` 的值）
* `.env.local`（当 `NODE_ENV=test` 时不会加载）

```ini .env icon="settings" theme={"theme":{"light":"github-light","dark":"dracula"}}
FOO=hello
BAR=world
```

***

变量也可以通过命令行设置。

<CodeGroup>
  ```sh Linux/macOS icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
  FOO=helloworld bun run dev
  ```

  ```sh Windows icon="windows" theme={"theme":{"light":"github-light","dark":"dracula"}}
  # 使用 CMD
  set FOO=helloworld && bun run dev

  # 使用 PowerShell
  $env:FOO="helloworld"; bun run dev
  ```
</CodeGroup>

***

有关在 Bun 中使用环境变量的更多信息，请参见 [文档 > 运行时 > 环境变量](/runtime/environment-variables)。
