简体   繁体   English

是否可以在 json 文件中存储 dotenv 变量?

[英]Is it possible to store a dotenv variable in a json file?

Using Gatsby with Ghost CMS requires a.ghost.json file containing my API keys.在 Ghost CMS 中使用 Gatsby 需要一个包含我的 API 密钥的.ghost.json 文件。 I'd like to push the repo to Github and don't want my keys in my repository.我想将 repo 推送到 Github 并且不希望我的密钥在我的存储库中。 Hence the question: is it possible to use.env variables within json files?因此问题是:是否可以在 json 文件中使用 .env 变量?

By default, Gatsby looks for .env variables inside .env.development (or .env.production ) when you exposes the:默认情况下,当您公开以下内容时,Gatsby 会在.env.development (或.env.production )中查找.env变量:

require("dotenv").config({
  path: `.env.${process.env.NODE_ENV}`,
})

Of course, you can change this behavior.当然,您可以更改此行为。 If you want to keep your variables inside a .json file without pushing it, just add them to .gitignore and import them in the files you need ( gatsby-config.js or whatever) using a require function.如果您想将变量保存在.json文件中而不推送它,只需将它们添加到.gitignore并使用require function 将它们导入您需要的文件( gatsby-config.js或其他文件)中。 Using, for example: require('../../ghost.json') .使用,例如: require('../../ghost.json')

So, I would recommend using the default configuration to avoid possible issues.因此,我建议使用默认配置以避免可能出现的问题。 You can keep your file pushed without API keys and move them to the .env local file and simply load the where you need by: process.env.YOUR_API_KEY_VARIABLE您可以在没有 API 密钥的情况下保持文件推送并将它们移动到.env本地文件,然后只需通过以下方式加载您需要的位置: process.env.YOUR_API_KEY_VARIABLE

For further information: https://www.gatsbyjs.org/docs/environment-variables/更多信息: https://www.gatsbyjs.org/docs/environment-variables/

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

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