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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ttoohey/react-use-environment

v1.0.2

Published

React Hook to load runtime environment data from environment.json

Readme

useEnvironment

A React Hook to load runtime environment variables from a JSON link.

Why?

See issue #578 for some background.

Usage

Install

npm install @ttoohey/react-use-environment

Create an environment.json file in the public/ folder

# public/environment.json
{
  "EXAMPLE_VAR": "Test value",
  "ANOTHER": "Another value"
}

Add a <link> tag to public/index.html

# public/index.html
<html>
  <head>
    ...
    <!--
      environment.json provides runtime environment variables.
    -->
    <link
      id="environment"
      rel="preload"
      as="script"
      href="%PUBLIC_URL%/environment.json"
    />
    ...
  </head>
  <body>
    ...
  </body>
</html>

The environment can be loaded in any React component. For example, in src/index.js load an API_URL to pass to an ApolloClient instance.

// src/index.js
import React, { Suspense } from "react";
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "react-apollo-hooks";
import useEnvironment from "@gency/react-use-environment";

import App from "./App";
import Loading from "./Loading";

const Root = () => {
  const { API_URL } = useEnvironment();
  const client = new ApolloClient({ uri: API_URL });
  return (
    <ApolloProvider client={client}>
      <App />
    </ApolloProvider>
  );
};
ReactDOM.render(
  <React.Suspense fallback={<Loading />}>
    <Root />
  </React.Suspense>,
  document.getElementById("root")
);

A React Suspense component must be used as useEnvironment will throw a promise while loading the JSON file.

API

The useEnvironment function accepts an options argument containing the following entries:

  • linkId -- (default: "environment"). Use this to identify the id of the tag
  • suspense -- (default: true). Whether to use React Suspense for a fallback.

If suspense: false is used the return value contains three elements: [env, loading, error]

const [env, loading, error] = useEnvironment({ suspense: false });
if (loading) return "Loading...";
if (error) return "Error";
const { API_URL } = env;
// ... etc

Creating environment.json

Development

If possible, structure development environments so that each developer can use the same configuration. This way the environment.json file can simply be committed to the Git repository.

If that's not possible, creating a public/environment.json file from the "start" script would normally be sufficient.

Staging and Production

As part of the build step have the public/environment.json file replaced with a template file. This can be done in the build script in package.json

# src/environment.json
{
  "API_URL": "$API_URL"
}
# package.json
{
  "scripts": {
    "build": "react-scripts build && cp src/environment.json build/environment.json",
  }
}

When deploying the app replace the template file with one with the environment variables replaced.

For example, if creating a Docker image, something like the following can be used to do the environment variables substitution when the container starts.

# Dockerfile
FROM nginx:alpine
COPY build /usr/share/nginx/html
COPY build/environment.json /environment.json
RUN chown -R nginx:nginx /usr/share/nginx/html
CMD ["/bin/sh", "-c", "envsubst </environment.json >/usr/share/nginx/html/environment.json && nginx -g 'daemon off;'"]