Search code examples
javascriptjsongatsbyghost-blogdotenv

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. I'd like to push the repo to Github and don't want my keys in my repository. Hence the question: is it possible to use .env variables within json files?


Solution

  • By default, Gatsby looks for .env variables inside .env.development (or .env.production) when you exposes the:

    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. Using, for example: 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

    For further information: https://www.gatsbyjs.org/docs/environment-variables/