简体   繁体   中英

Why does destructuring next.js environment variables result in undefined?

I have created a.env file with api_key When destructuring the value

const { api_key } = process.env; // = undefined
const key = process.env.api_key; // = value

Can someone help explain this?

From 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.

and for client-side React components

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM