next.js: 10.0.8: Env variables redefined within next.config.js are undefined in dev mode
What version of Next.js are you using?
10.0.8
What version of Node.js are you using?
14.15.1
What browser are you using?
Tested in Firefox and Safari
What operating system are you using?
macOS Big Sur 11.2.1
How are you deploying your application?
yarn dev, yarn build && yarn start
Describe the Bug
Environment variables defined in next.config.js return undefined in page components when on dev mode (yarn dev).
But running the same project on production (yarn build && yarn start) the env variables work as expected.
Expected Behavior
If env variables are set in an .env file at the root of the project and then exposed to Next.js within the next.config.js file I would expect them to be accessible in component code in both production and development environments.
To Reproduce
- Clone example project. https://github.com/krall12/nextjs-env-config-dev-bug
- Install deps
- run yarn dev and open browser. You should see the test environment variable not defined.
- run yarn build && yarn start and open your browser. You should see the test environment variable correctly defined.
As a control:
- yarn add next@10.0.7
- in both dev and production modes the environment variable is returned correctly.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 18
- Comments: 29 (5 by maintainers)
Hi, this should be corrected in
v10.0.9-canary.2of Next.js, please upgrade and give it a try!I’ve been accessing values in
.env.localfromnext.config.jsto configure myimage.domainsfor the newnext/imagecomponent.This used to work before the upgrade to
10.0.8. It now returnsundefined.Try this version
10.0.8-canary.9+ bigger than canary 9, all of the env files are delayed loaded.
I solved this by prefixing all env variables that I need on the browser with NEXT_PUBLIC_
@ijjk works great now, thanks!