next.js icon indicating copy to clipboard operation
next.js copied to clipboard

process.env variables are undefined at frontend

Open nitz-iron opened this issue 1 year ago • 1 comments

Verify canary release

  • [X] I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 Binaries: Node: 18.11.0 npm: 8.19.2 Yarn: N/A pnpm: N/A Relevant packages: next: 13.0.3-canary.1 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0

What browser are you using? (if relevant)

Chrome

How are you deploying your application? (if relevant)

next export

Describe the Bug

env variables are undefined in the frontend tho I used PUBLIC_NEXT prefix.

question: Can I also use env variables in next export?

Expected Behavior

env variables are not undefined in frontend

Link to reproduction

https://github.com/nitz-iron/next-env-var-undef

To Reproduce

  1. npm start
  2. enter to "/" path
  3. in chrome console: process.env is an empty object / process.env.NEXT_PUBLIC_GANAV = undefined

nitz-iron avatar Nov 08 '22 15:11 nitz-iron

I cloned your repo, created an environment variable in .env.local called NEXT_PUBLIC_GANAV=Hello and it works. Could there be a mistake in your .env.local file.

Screenshot 2022-11-08 at 12 18 07 PM

oelbaga avatar Nov 08 '22 17:11 oelbaga

You're right (I am the author from different user). I see it works if I omit the && from the npm start command. But what about next export? how can I do it there via npm command?

ghost avatar Nov 08 '22 18:11 ghost

I succeeded to do it like this:

image

tomorrow I'll check if it works on next served via custom express server, pls do not close.

ghost avatar Nov 08 '22 18:11 ghost