Category: Next.js, React and TypeScript

Next.js: process.env runs on the server but undefined in the browser

Read this guide in Polish

The API variable exists while rendering the server, and in Client Component reads undefined. Next.js does not provide normal server-only variables to the browser code; public must be clearly marked.

How to determine the source of the error

Check whether the reading is in the use client file and whether the name starts with NEXT PUBLUC .

Verify the target build environment and whether the new value has been included in the new deployment.

What to do step by step

1. Do not move a secret key to your client; if necessary, read it in Route Handler and return only the required result.

2 . For the open interface configuration set NEXT PUBLUC before build and rebuild the application.

How to check the score

The public value is available in the new Bundle, and the secret key does not appear in the JavaScript code downloaded by the browser.

What to do

Do not add NEXT PUBLAC prefix to private keys; this means revealing them.