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

react-dotenv

v0.1.3

Published

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

Downloads

29,150

Readme

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>;
}