npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gatsby-env-variables

v2.0.0

Published

Use your env variables in client side

Downloads

10,577

Readme

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 environment.js files inside env/ folder, on root of your project, file index.js will be the file with variables shared between multiple environments, if you chose other env, these variables will be merged

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

index.js

module.exports = {
  API_ROOT: "example.com",
  CARDS: "/cards",
}

staging.js

module.exports = {
  API_ROOT: "stg.example.com",
}

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

Global variables

/* globals API_ROOT, CARDS */

function Example() {
  const cardsURL = API_ROOT + CARDS // stg.example.com/cards
  fetch(cardsURL)
}

If you don't want to use /* globals */ in each file, just create an empty .eslintrc file in your project folder. If you are using eslint, just disable the no-undef rule.

Importing variables

import { API_ROOT, CARDS } from "gatsby-env-variables"

function Example() {
  const cardsURL = API_ROOT + CARDS // stg.example.com/cards
  fetch(cardsURL)
}

Using themes

You can have multiple themes, with multiple environments, just put your variables inside the name of theme, and use THEME=example on your running script

staging.js

module.exports = {
  API_ROOT: "stg.example.com",
  dark: {
    CARDS: "/dark_cards",
  }
}

package.json

THEME=dark BUILD_ENV=staging yarn start

Use in client-side

function Example() {
  const cardsURL = API_ROOT + CARDS // stg.example.com/dark_cards
  fetch(cardsURL)
}

Nested Objects

String values are not required, you can use nested objects too

staging.js

module.exports = {
  API: {
    CARDS: "/cards"
  }

  dark: {
    API_ROOT: "darkexample.com"
  }
}

Async variables

If you have to put dynamic variables in you environment, like values coming from API or something like this, you can export an promise

staging.js

module.exports = new Promise(async res => {
  const ROOT_API = "example.com"
  const CARDS = "/cards"

  const response = await fetch(ROOT_API + CARDS)
  const ACTIVE_CARDS = await response.json()

  const envs = {
    ROOT_API,
    CARDS,
    ACTIVE_CARDS,
    dark: {
      CARDS: "/dark_cards",
    },
    orange: {
      CARDS: "/orange_cards",
    },
  }

  res(envs)
}) 

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/
    ├── index.js
    ├── development.js
    ├── staging.js
    └── production.j

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/
  ├── index.js
  ├── development.js
  ├── staging.js
  └── production.jn

Further reading

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