Socket
Socket
Sign inDemoInstall

gatsby-env-variables

Package Overview
Dependencies
1
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    gatsby-env-variables

Use your env variables in client side


Version published
Maintainers
1
Install size
40.0 kB
Created

Readme

Source

Version Downloads Total

gatsby-env-variables

Webpack feature to provide your custom environment env in client side Use BUILD_ENV to chose current .env file

Install

$ npm i gatsby-env-variables

or

$ yarn add gatsby-env-variables

How to use

Add the plugin to your gatsby-config.js.

module.exports = {
  plugins: [
    `gatsby-env-variables`
  ]
}

Create your's .env files inside env/ folder, on root of your project, file .env will be default variable values, if you chose other env, these variables will be merged

project/
├── env/
  ├── .env
  ├── .env.development
  ├── .env.staging
  └── .env.production

.env

COMMON=default

.env.staging

COMMON=staging
HAS_HEADER=true

Run your yarn/npm script with BUILD_ENV variable to chose your environment, default selected is development

package.json

BUILD_ENV=staging yarn start

Use in client-side

/* globals HAS_HEADER, COMMON */

function Example() {
  console.log(COMMON) // staging
  return HAS_HEADER && <Header />
}

Options

envFolderPath

This options allows you to specify which folder will stay your .env files

Example:

module.exports = {
  plugins: [
    {
      resolve: `gatsby-env-variables`,
      options: {
        envFolderPath: `src/env/`
      }
    }
  ]
}
project/
├── src/
  ├── env/
    ├── .env
    ├── .env.development
    ├── .env.staging
    └── .env.production

Or you can use this option to rename to config/ folder too

Example:

module.exports = {
  plugins: [
    {
      resolve: `gatsby-env-variables`,
      options: {
        envFolderPath: `config/`
      }
    }
  ]
}
project/
├── config/
  ├── .env
  ├── .env.development
  ├── .env.staging
  └── .env.production

Further reading

Check out the DefinePlugin section of the Webpack config documentation for more information.

Keywords

FAQs

Last updated on 19 Dec 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc