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

# 设置环境变量

使用 `process.env` 或 `Bun.env` 访问当前环境变量。

```ts index.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
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={null}
FOO=hello
BAR=world
```

***

你也可以在命令行上设置变量。

<CodeGroup>
  ```sh Linux/macOS icon="terminal" theme={null}
  FOO=helloworld bun run dev
  ```

  ```sh Windows icon="windows" theme={null}
  # 使用 CMD
  set FOO=helloworld && bun run dev

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

***

参见 [环境变量](/runtime/environment-variables)。
