gatsby-plugin-config
Plugin for gatsby to handle environment variables with ease.
- Handle empty strings
- Strip out GATSBY_ prefix
Install
Install with npm or yarn
npm install gatsby-plugin-config dotenv -S
In absolute top of your gatsby-config.js
:
// Initialize dotenv; // And then you can use the config in gatsby-config.jsconst config = ;
Usage
Import the plugin with import
;
GATSBY variables
Variables prefixed with GATSBY_
are accessiable from the client side by default. This plugin allows you to query them without the prefix.
// process.env.GATSBY_API_URLconfigAPI_URL
Empty variables
You don't have to match empty variables anymore like this:
if processenvGATSBY_IS_STAGING && processenvGATSBY_IS_STAGING !== '' // do something
You can now just do:
if configIS_STAGING // you're done
gatsby-config.js
Usage in files without module system (like gatsby-config / gatsby-node etc.)
const config = default; moduleexports = siteMetadata: title: configSITE_TITLE ;
Dotenv
You can keep separate dotenv files for each environment in the project root. See https://github.com/motdotla/dotenv for more details.
.env.development
.env.staging
.env.production
The format is simply .env.${process.env.NODE_ENV}
Troubleshooting
Please file a bug if any issues occour.