英文:
How to pick up `.vercel` environment variables when running Next locally
问题
使用vercel env pull时,它会将一个环境文件放在.vercel文件夹中。例如,vercel env pull --environment=production将创建.vercel/env.production.local文件。
我想要运行下一个开发服务器或本地生产构建,以便它使用这些环境变量,但在运行next dev或next start时,它似乎只会读取存储库根目录中的环境文件。
我如何让这些命令读取.vercel目录中提取的文件?或者这些环境文件不是用来这样使用的吗?
英文:
When using vercel env pull it places an env file in the .vercel folder. For example vercel env pull --environment=production will create .vercel/env.production.local.
I would like to run the next dev server or local production build so that it uses those env variables, but when running next dev or next start it seems to only pick up env files from the repository root.
How can I make those commands pick up the pulled files from the .vercel directory? Or where these env files not meant to be used like that?
答案1
得分: 1
使用vercel env pull命令和--environment标志时,它会在.vercel文件夹中创建一个.env.production.local文件,就像你说的那样。但默认情况下,Next.js 不会自动从该文件夹中获取环境变量。要让 Next.js 从.vercel文件夹中获取环境变量,你需要进行如下配置:
- 在你的 Next.js 项目根目录中创建一个
next.config.js文件。 - 如果还没有安装 dotenv 包,请运行以下命令进行安装:
npm install dotenv --save-dev。 - 在
next.config.js文件中,你可以使用 dotenv 包来加载来自所需位置的环境变量。
在你的 next.config.js 文件中使用以下代码:
const path = require('path');
require('dotenv').config({ path: path.resolve(process.cwd(), '.vercel/env.production.local') });
然后,你可以在 module.exports 中添加你的配置信息。
英文:
When using vercel env pull with the --environment flag, it creates an .env.production.local file in the .vercel folder like you said. But by default, Next.js doesn't automatically get environment variables from that folder. To make Next.js pick up environment variables from the .vercel folder, you need to configure it to do so:
- Create a next.config.js in the root of your Next.js project
- Install the dotenv package if you haven't done so already:
npm install dotenv --save-dev. - Inside next.config.js, you use the dotenv package to load environment variables from the desired location.
Use the following code in your next.config.js file:
const path = require('path');
require('dotenv').config({ path: path.resolve(process.cwd(),'.vercel/env.production.local') });
You can then add your configurations in module.exports.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论