Giter VIP home page Giter VIP logo

react-dotenv's Introduction

react-dotenv ๐ŸŽ›

Load environment variables dynamically for your React applications created with CRA (Create-React-App).

Installation

npm install react-dotenv

Usage

1. Setup your project

Open your project's package.json file and:

  1. Add an .env file to your project root (or just load from the system environment variables).
  2. Add the react-dotenv NPM command to your start, build (and your serve commands).
  3. Add the react-dotenv.whitelist property to package.json to specify which variables you need exposed.

Example

package.json:

{
  "name": "my-react-app",
  "version": "0.1.0",
  "dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-dotenv": "^0.1.0",
    "react-scripts": "3.4.3",
    "serve": "^11.3.2"
  },
  "scripts": {
    "start": "react-dotenv && react-scripts start", // <-- append command
    "build": "react-dotenv && react-scripts build", // <-- append command
    "serve": "react-dotenv && serve build", // <-- append command
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [">0.2%", "not dead", "not op_mini all"],
    "development": ["last 1 chrome version", "last 1 firefox version", "last 1 safari version"]
  },
  // Add the react-dotenv configuration
  "react-dotenv": {
    "whitelist": ["API_URL"]
  }
}

2. Run your project

npm start

Now your project have the environment variables loaded globally in the window.env property.

3. Read the environment variables

You can access the environment variables from your code in two ways:

A. Using the react-dotenv library

import React from "react";
import env from "react-dotenv";

export function MyComponent() {
  return <div>{env.API_URL}</div>;
}

B. Using the window.env global variable

import React from "react";

export function MyComponent() {
  return <div>{window.env.API_URL}</div>;
}

react-dotenv's People

Contributors

jeserodz avatar jmcudd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-dotenv's Issues

fetching index.html instead of env.js in non-root routes

Hi Jese, thank you for writing this package! I am teaching a coding bootcamp, and I am having my students use this to manage their env vars locally and on heroku. We are also using react-router-dom for routing and navigation. (I am not an experienced react or webpack developer, so please tell me if I am doing anything silly!)

I've noticed that when navigating to a route other than root, the <script id="react-dotenv" src="./env.js"></script> tag makes a request to the relative route hostname/yourCurrentRoute/env.js
image

And the server fulfills this request with index.html, which of course blows up
image

I've been working around it by adding "homepage": "" to my package.json, so that the #react-dotenv tag in my index.html gets a src of "/env.js" instead of "./env.js". This seem to fix the issue, as the request uses an absolute route and always goes to hostname/env.js, no matter the route of my react app. Should "" be the default for homepage instead of "."?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.