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

embed-react-app-envs

v0.0.6

Published

Script for embedding environment variable in CRA apps without having to rebuild the on the server.

Downloads

7

Readme

Motivation

Create react app provides no official way to inject environment variable from the server into the page.
When you run yarn build create react app does bundle all the variables prefixed by REACT_APP_ and expose them under process.env (see here).
The problem, however, is that you likely don't want to build your app on the server.
The CRA team also suggests to introduce placeholders in the public/index.html and do the substitution on the server before serving the app. This solution involves a lot of hard to maintain scripting.

This module abstract away the burden of managing environment variable injection as well as providing a type-safe way to retrieve them in your code.

Step by step guide

Start by installing the tool:

yarn add embed-react-app-envs 

Then declare all the allowed environment variables into the .env file of your project

Example

REACT_APP_FOO="Default value of foo"
REACT_APP_BAR=
REACT_APP_BAZ=
REACT_APP_FIZZ=

Once it's done run the script npx generate-typed-env-getter ( Use npx generate-typed-env-getter js if you your project don't use TypeScript)

It will generate src/env.ts ( or src/env.js) looking like:

/* 
 * Automatically generated by embed-react-app-envs.
 * If you wish to declare a new environment variable add it in the .env file
 * then run 'npx generate-typed-env-getter'. This file will be updated.
 */
import { getEnvVarName } from "embed-react-app-envs";

export function getEnv() {
    return {
        "FOO": getEnvVarName("FOO"),
        "BAR": getEnvVarName("BAR"),
        "BAZ": getEnvVarName("BAZ"),
        "FIZZ": getEnvVarName("FIZZ")
    } as const;
}

Now let's test it by creating a .env.local file like:

REACT_APP_BAR="Value of bar defined in .env.local"

And let's do this somewhere in our code:

import { getEnv } from "./path/to/env.ts"

console.log(getEnv());

Now if we run REACT_APP_BAZ="Value of baz passed inline" yarn start we get this in the console:

{
    "FOO": "Default value of foo",
    "BAR": "Value of bar defined in .env.local",
    "BAZ": "Value of baz passed inline",
    "FIZZ": ""
}

Now if you run yarn build then REACT_APP_BAZ="Value of baz on the server" npx embed-environnement-variables the value of REACT_APP_BAZ will be injected in build/index.html so that if you start statically serving the build/ dir, for example with npx serve you will get this in the console:

{
    "FOO": "Default value of foo",
    "BAR": "Value of baz on the server",
    "BAZ": "",
    "FIZZ": ""
}

Setup

package.json

"scripts": {
    "postinstall": "generate-typed-env-getter",
    "prestart": "generate-typed-env-getter",
    "pretest": "generate-typed-env-getter"
}

NOTE: Those scripts are optional, as long as you remember to rerun npx generate-typed-env-getter each time you update .env you are good.

.gitignore

/src/env.ts

Dockerfile

# build environment
ENTRYPOINT sh -c "npx embed-environment-variables && mv build /usr/share/nginx/html && nginx -g 'daemon off;'"

See complete Dockerfile example. TODO: Link example