简体   繁体   English

Vercel next.js 环境变量不起作用

[英]Vercel next.js environment variable not working

I'm using google maps in a Next.js project and everything's working locally.我在 Next.js 项目中使用谷歌地图,一切都在本地工作。 The google maps secret key is being stored in next.config.js which I access in the code through process.env.NEXT_PUBLIC_GOOGLEMAPS谷歌地图密钥存储在 next.config.js 中,我通过 process.env.NEXT_PUBLIC_GOOGLEMAPS 在代码中访问它

When I deployed the project to Vercel, I didn't include next.js.config for security reasons.当我将项目部署到 Vercel 时,出于安全原因,我没有包含 next.js.config。 So I added the key under Settings -> Environment Variables所以我在 Settings -> Environment Variables 下添加了密钥在此处输入图像描述

However, the environment variable is not working in production.但是,环境变量在生产中不起作用。 When I console log process.env.NEXT_PUBLIC_GOOGLEMAPS, I get undefined当我控制台记录 process.env.NEXT_PUBLIC_GOOGLEMAPS 时,我得到未定义

What could be causing this issue and how can I fix it?什么可能导致此问题,我该如何解决? Thanks谢谢

This is how I got my setup to work...这就是我让我的设置工作的方式......

  1. I have an env.local file for local dev key.我有一个本地开发密钥的env.local文件。 Add .env.local.* to.gitignore This file has my key/value pair like so:.env.local.*添加到 .gitignore这个文件有我的键/值对,如下所示:

1 NEXT_PUBLIC_G_KEY=AFLkefjlkwblahblahblah

  1. I have a next.config.js file that has this key as well in an env space:我有一个env文件,它在环境空间中也有这个键:

env: { NEXT_PUBLIC_G_KEY: process.env.NEXT_PUBLIC_G_KEY } (process.env works out of the box according to the docs ) env: { NEXT_PUBLIC_G_KEY: process.env.NEXT_PUBLIC_G_KEY } (process.env 根据文档开箱即用)

  1. I deploy to vercel so I create a secret with the actual value of the key...:我部署到vercel,所以我用密钥的实际值创建了一个秘密......:

$> yarn now secret add MyAppName_PRoD_G_KEY AFLkefjlkwblahblahblah

  1. i go to my app in vercel > settings > environment variables and create a new one in the UI, mapped to the secret i just made in the command line.我将 go 到我的应用程序中 vercel > 设置 > 环境变量并在 UI 中创建一个新的,映射到我刚刚在命令行中创建的秘密

映射到机密的 vercel 环境变量

I have the similar problem.我有类似的问题。 In my env variable list of Vercel dashboard I have used vercel url value but it is picking up the old value of url.在 Vercel 仪表板的 env 变量列表中,我使用了 vercel url 值,但它正在使用 url 的旧值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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