Skip to content

Support Bun frontend process.env prefixing (e.g. BUN_PUBLIC_*) #181

@yamcodes

Description

@yamcodes

When using arkenv in a Bun + React project, process.env is not defined in the frontend runtime. Bun only exposes environment variables that are explicitly prefixed (default: BUN_PUBLIC_*) and accessing process.env directly throws a runtime error.

Additionally, during the build step, Bun's bundler defaults to browser builds, which causes import errors such as:

import { styleText } from "util";
             ^
error: Browser polyfill for module "node:util" doesn't have a matching export named "styleText"

Steps to Reproduce:

  1. Create a clean React template with bun init.
  2. Install arkenv.
  3. Import and call createEnv.
  4. Run bun serve → runtime error: process is not defined.
  5. Run bun build → bundler error on styleText.

Expected behavior:

arkenv should support Bun's process.env model, allowing retrieval of all variables with a given prefix (e.g., BUN_PUBLIC_*).

Workaround:

Manually construct the env object and pass only the prefixed vars to the schema:

import { type } from "arktype"

const envType = type({ MY_VAR: "string" })
const env = envType.assert({ MY_VAR: process.env.BUN_PUBLIC_MY_VAR })
export default env

Related:

Action Items:

  • Add support for collecting env vars by a configurable prefix (default: BUN_PUBLIC_*).
  • Ensure builds targeting browser/bun don't attempt to import Node-only utilities like util.styleText.

Metadata

Metadata

Labels

@arkenv/bun-pluginIssues or Pull Requests involving the Bun plugin for ArkEnvenhancementNew feature or requesthelp wantedExtra attention is needed

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions