stack/packages/stack-shared/src
BilalG1 b3d0ab66cc
fix(stack-shared): make process.env access browser-safe (#1391)
## Summary

- Bare `process.env.X` accesses in `stack-shared` throw `ReferenceError:
process is not defined` when the package is bundled into a browser app
without a `process` shim (e.g. a plain Vite app). The most reachable
offenders are in `StackAssertionError`'s constructor and
`schema-fields.ts`'s Neon Basic-auth validator, both of which can run on
the client during normal sign-in flows with `@stackframe/react`.
- Extracted a zero-dependency `getProcessEnv` helper at
`packages/stack-shared/src/utils/process-env.tsx` and routed the bare
references through it. Returns `undefined` when `process` is not
defined; otherwise behaves like a normal `process.env[name]` read, so
Next.js/webpack inlining is unchanged on the server.
- Touched: `schema-fields.ts:884` (`STACK_INTEGRATION_CLIENTS_CONFIG`),
`utils/errors.tsx:81` (`NEXT_PUBLIC_STACK_DEBUGGER_ON_ASSERTION_ERROR`),
`utils/promises.tsx` (`NODE_ENV` in `runAsynchronouslyWithAlert`),
`utils/esbuild.tsx:16` (`NODE_ENV`, also reordered the `typeof process`
guard so the env access is unreachable in browsers).

## Why a separate helper module

`utils/env.tsx` already exists but its `getEnvVariable` explicitly
throws in the browser, so it can't be reused here. The new module has
zero imports so it can be safely consumed from low-level utilities like
`errors.tsx` without creating a cycle (env.tsx ↔ errors.tsx).

## Test plan

- [x] `pnpm lint` passes
- [x] `pnpm typecheck` passes
- [ ] Reproduced the original failure in a Vite + `@stackframe/react`
app: sign-in flow logged `ReferenceError: process is not defined` from
`StackAssertionError`, plus `clientSecret must not be empty` cascading
from the same path
- [ ] Verify the same flow in a Vite app no longer throws once
`@stackframe/react` is rebuilt against this `stack-shared` change
- [ ] Confirm Next.js consumer behavior is unchanged (env vars still
inlined at build time for `NEXT_PUBLIC_*`)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Release Notes

* **Refactor**
* Improved environment variable handling across shared utilities for
enhanced browser compatibility and safety. Introduced a new utility for
dynamic, browser-safe environment variable access that prevents errors
in non-Node.js environments.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 10:59:49 -07:00
..
ai Overview revamp (#1238) 2026-04-15 09:36:00 -07:00
apps Fraud Protection sub-app 2026-04-05 21:35:01 -07:00
config clickhouse new syncs and verify-data (#1304) 2026-04-08 14:43:22 -07:00
helpers fix(init-prompt): require StackProvider for all frameworks (#1374) 2026-04-24 12:27:49 -07:00
hooks Payments UX update (#863) 2025-08-27 17:28:01 -07:00
interface Dashboard: DataGrid refactor + layout (stacked on overview-revamp) (#1338) 2026-04-27 13:50:24 -07:00
payments [Feat] Add payment methods page to dashboard (#1103) 2026-01-20 14:33:31 -08:00
utils fix(stack-shared): make process.env access browser-safe (#1391) 2026-04-28 10:59:49 -07:00
config-authoring.test.ts make config typesafe (#1254) 2026-04-06 18:31:55 +00:00
config-authoring.ts make config typesafe (#1254) 2026-04-06 18:31:55 +00:00
config-rendering.ts make config typesafe (#1254) 2026-04-06 18:31:55 +00:00
crud.tsx Config DB (#568) 2025-04-10 19:38:25 +02:00
global.d.ts Replace required() of yup schemas with defined() 2024-11-18 20:08:55 +01:00
index.ts [docs][dashboard][stack-shared] Update docs to new apps (#996) 2025-11-12 15:45:30 -06:00
known-errors.tsx Better error message when user info fetching fails 2026-04-13 11:10:32 -07:00
plans.ts [Fix] [Refactor] Implement Base Settings for Stack-Auth Plans and Move Metadata from Stripe Webhook Event to Table (#1214) 2026-02-23 22:09:27 -08:00
schema-fields.ts fix(stack-shared): make process.env access browser-safe (#1391) 2026-04-28 10:59:49 -07:00
sessions.ts Update AGENTS.md 2026-02-13 18:34:23 -08:00