gpt4 book ai didi

javascript - 为什么在 Next.js 中未定义解构的环境变量?

转载 作者:行者123 更新时间:2023-12-05 00:28:45 26 4
gpt4 key购买 nike

在我的 Next.js 应用程序中,我创建了一个 .env带有名为 API_KEY 的变量的文件.
解构值会导致 undefined ,如此处所示:

const { API_KEY } = process.env; // = undefined
const key = process.env.API_KEY; // = 'value'
谁能解释一下原因 process.env此处未填充正确的值?

最佳答案

来自 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 that process.env is not a standard JavaScript object, so you’re not able to use object destructuring.


对于客户端 React 组件

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 various NEXT_PUBLIC_ envs need to be set when the project is built.

关于javascript - 为什么在 Next.js 中未定义解构的环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67566552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com