Skip to content

Create-react-app library to allow server-side runtime environment variables to be made available to client-side scripts

Notifications You must be signed in to change notification settings

codenstein/react-client-variables

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React Client Variables Plugin

A Create-React-App library to allow server-side runtime environment variables to be made available to client-side scripts.

By default, Create-react-app only makes system environment variables prefixed with REACT_APP_ available to client scripts. Using this library, you can make server-side runtime environment variables, prefixed with REACT_APP_, available within the browser i.e., env variables passed to docker container.

Install

npm install react-client-variables

How to use

In package.json:

{
  "scripts": {
    "build": "react-scripts build && react-client-variables",
  }
}

Client-side:

import variables from "react-client-variables";
import { API_URL } from "react-client-variables";

Server-side:

const initClientVariables = require("react-client-variables/server");

async function main() {
  const buildDir = path.resolve("build");

  await initClientVariables({ buildDir });

  app.use(express.static(buildDir));
  app.get("/", function (req, res) {
    res.sendFile(path.join(buildDir, "index.html"));
  });

  app.listen(3000, function () {
    console.log("App started on port 3000");
  });
}

main();

About

Create-react-app library to allow server-side runtime environment variables to be made available to client-side scripts

Resources

Stars

Watchers

Forks

Packages

No packages published