Skip to content

fix(auth): guard process.env access for browser/Vite compatibility#483

Open
peteroyce wants to merge 1 commit intodeepgram:mainfrom
peteroyce:fix/guard-process-env-browser
Open

fix(auth): guard process.env access for browser/Vite compatibility#483
peteroyce wants to merge 1 commit intodeepgram:mainfrom
peteroyce:fix/guard-process-env-browser

Conversation

@peteroyce
Copy link
Copy Markdown

Summary

HeaderAuthProvider directly accesses process.env without checking whether the global process object exists. In browser environments (Vite, Webpack without a process polyfill, Parcel, Cloudflare Workers, Deno), this throws a ReferenceError at runtime:

ReferenceError: process is not defined

Root Cause

Optional chaining (process.env?.KEY) only guards against env being null/undefined on an existing process object. If process itself is not defined as a global, the expression throws before optional chaining can help.

Fix

Wrap both process.env accesses with typeof process !== "undefined" guards:

- return options?.[PARAM_KEY] != null || process.env?.[ENV_HEADER_KEY] != null;
+ return options?.[PARAM_KEY] != null || (typeof process !== "undefined" && process.env?.[ENV_HEADER_KEY] != null);

- const headerValue = (await core.Supplier.get(this.options[PARAM_KEY])) ?? process.env?.[ENV_HEADER_KEY];
+ const headerValue = (await core.Supplier.get(this.options[PARAM_KEY])) ?? (typeof process !== "undefined" ? process.env?.[ENV_HEADER_KEY] : undefined);

typeof checks never throw regardless of the runtime environment, making this safe in Node.js, browsers, edge runtimes, and any environment that doesn't define process.

Testing

Existing behaviour is unchanged in Node.js. In browser environments the SDK now gracefully falls back to undefined when DEEPGRAM_API_KEY cannot be read from process.env, instead of crashing.

Closes #349

Direct access to process.env throws ReferenceError in environments where
the global process object does not exist, such as browser builds with
Vite, Webpack, or Parcel without a process polyfill.

Wrap both process.env accesses in HeaderAuthProvider with a
typeof process !== "undefined" guard so the SDK works seamlessly
in browser, Node.js, and edge runtimes without requiring a polyfill.

Closes deepgram#349
@peteroyce peteroyce requested a review from lukeocodes as a code owner April 11, 2026 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Direct call to process.env in Abstract Client

1 participant