cloudflare / cloudflare/vinext

next/constants shim throws ReferenceError: process is not defined in client bundles

Open Beginner friendly
#2,661 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
8.8k
Forks
406
Avg merge
2d 6h
Merged PRs (30d)
120

Description

### Summary

Importing anything from `next/constants` in client code crashes production browser bundles with `ReferenceError: process is not defined`.

The shim (`packages/vinext/src/shims/constants.ts`) has a top level bare reference to `process`:

```js
export const CONFIG_FILES = [
"next.config.js",
"next.config.mjs",
"next.config.ts",
// process.features can be undefined on Edge runtime
...(process?.features?.typescript ? ["next.config.mts"] : []),
];
```

Optional chaining does not guard an undeclared identifier, so evaluating the module in a browser throws. Real Next.js has the same expression in its constants module but survives because webpack injects a `process` polyfill into client bundles. Vite does not polyfill `process`.

Since `next/constants` is aliased for every environment (public-shim-map applies to the shared `resolve.alias`), the shim is a legitimate client import. `@sentry/nextjs` for example imports `PHASE_PRODUCTION_BUILD` in code that ends up in the client graph. And because the shim lands in the shared `vinext-*` client manual chunk, the failed evaluation takes down that whole runtime chunk, not just the importer. In our case the visible symptom was Sentry's client SDK never initializing in production.

### Reproduction

Add a `"use client"` page that imports `PHASE_PRODUCTION_BUILD` from `next/constants` to the `app-basic` fixture, build for production, open in a real browser: the page logs `ReferenceError: process is not defined` (we saw 3 page errors from the shared chunk).

### Suggested fix

```js
...(typeof process !== "undefined" && process.features?.typescript ? ["next.config.mts"] : []),
```

Behavior is unchanged in any runtime that has `process`, and the module becomes safe to evaluate in the browser. Verified locally: 0 page errors after the change, same rendered output.

Happy to send a PR. Thanks for vinext, it has been great to run Next apps on Vite!

Contributor guide

Open the contributing guide

Research direction

Start in packages/vinext/src/shims/constants.ts, then inspect the app-basic fixture and its client production-build setup. Reproduce the failure in a real browser and verify the completed change produces no page errors while preserving the rendered output.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, typescript, vite
Domain
build-system, frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
82/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.