英文:
How to manage process.env.NODE_ENV on Vercel
问题
我在Vercel上有一个Next Js应用程序,并且已经设置了与数据库等相关的各种环境变量,既用于开发又用于生产。所有这些变量都起作用。
但我无法弄清楚如何在开发构建中使用我在本地环境中使用的process.env.NODE_ENV。例如,如果我想在开发环境中更改文本标志,它不起作用(在本地可以,但在Vercel上不行)。
<span>{process.env.NODE_ENV === "development" ? `${siteConfig.name} DEV` : siteConfig.name}</span>
这是正确的方法吗?例如,如何根据环境更改用户界面?
谢谢。
英文:
I have a Next Js app on Vercel and I've set various environment variables related to the database, etc, both for development and for production. All these variables work.
But I can't figure out how I can use the process.env.NODE_ENV that I use in local env in the dev build. For example, if I want to change the text logo in dev it doesn't work (in local yes, but on Vercel no)
<span>{process.env.NODE_ENV === "development" ? `${siteConfig.name} DEV` : siteConfig.name}</span>
I set the dev branch for preview.
I also tried to set these variables in this way, but of course, it doesn't work
Is that the correct way to approach this thing? How can I change, for example, the ui based on the environment?
Thanks
答案1
得分: 3
运行 next build
会自动设置 NODE_ENV=production
。
由于在 Vercel 上的生产和预览部署都运行 next build
,它们都会自动设置 NODE_ENV=production
,因为 next build
执行生产优化,其中之一是确保 React 使用生产版本。
我建议针对您的用例使用不同的环境变量名称,例如 SITE_NAME=Dev
。然后,您可以像以下方式编写您的代码:
<span>{process.env.SITE_NAME || siteConfig.name}</span>
英文:
Running next build
sets NODE_ENV=production
automatically.
Since both Production and Preview deployments on Vercel run next build
, they both set NODE_ENV=production
automatically because next build
performs production optimizations, one of those is ensuring React uses the production version.
I suggest using a different env var name for your use case such as SITE_NAME=Dev
. Then you can write your code like the following:
<span>{process.env.SITE_NAME || siteConfig.name}</span>
答案2
得分: 2
在Vercel文档中提到:
> 如果未分配环境变量NODE_ENV
,则在运行next dev
命令时,Next.js会自动分配development
,在运行所有其他命令时会分配production
。
在您的情况下,考虑完全避免使用NODE_ENV
,而是使用Vercel系统变量,特别是VERCEL_ENV
,它告诉您应用程序部署和运行的Vercel环境。
英文:
Vercel docs state:
> If the environment variable NODE_ENV
is unassigned, Next.js
> automatically assigns development
when running the next dev
> command, or production
for all other commands.
In your case, consider avoiding the use of NODE_ENV
entirely and instead use Vercel System Variables; particularly VERCEL_ENV
which tells you the Vercel Environment that the app is deployed and running on.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论