admin管理员组文章数量:1318335
In my Next.js app I've created a .env
file with a variable named API_KEY
.
Destructuring the value results in undefined
, as shown here:
const { API_KEY } = process.env; // = undefined
const key = process.env.API_KEY; // = 'value'
Can someone please explain why process.env
isn't populated with the correct value here?
In my Next.js app I've created a .env
file with a variable named API_KEY
.
Destructuring the value results in undefined
, as shown here:
const { API_KEY } = process.env; // = undefined
const key = process.env.API_KEY; // = 'value'
Can someone please explain why process.env
isn't populated with the correct value here?
-
Remove the
{}
and try again.{}
are used when there are multiple imports – Himanshu Saxena Commented May 17, 2021 at 8:49 - @HimanshuSaxena This doesn't explain why node result is undefined. This is es6 usage - using destructuring and otherwise works on everything else. – Inspiraller Commented May 17, 2021 at 8:56
- Can you show the actual code you tried? I mean you used some console.log, right? Or how do you know the api_key is undefined? Can you create minimal example which proves this? – Molda Commented May 17, 2021 at 9:32
-
Both lines should work the same. How exactly are you running this script? In what environment? How do you load the
.env
file? Are you using a transpiler/build pipeline? – Bergi Commented May 17, 2021 at 10:47 - @Bergi I am using Next.js. My console.log is in my react ponent. – Inspiraller Commented May 17, 2021 at 11:46
1 Answer
Reset to default 10From the docs:
Note: In order to keep server-only secrets safe, Next.js replaces
process.env.*
with the correct values at build time. This means thatprocess.env
is not a standard JavaScript object, so you’re not able to use object destructuring.
and for client-side React ponents
The value will be inlined into JavaScript sent to the browser because of the
NEXT_PUBLIC_
prefix. This inlining occurs at build time, so your variousNEXT_PUBLIC_
envs need to be set when the project is built.
本文标签: javascriptWhy are destructured environment variables undefined in NextjsStack Overflow
版权声明:本文标题:javascript - Why are destructured environment variables undefined in Next.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742042603a2417617.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论