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

@ludovicm67/react-dotenv

v1.0.7

Published

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

Downloads

192

Readme

react-dotenv

Load environment variables dynamically for your React applications created with create-react-app.

This will create a env.js file in your public and build directories, which will expose your environment variables you want in the global window.env variable. It will also take care of configuring the index.html file present in those directories to load that file.

Installation

npm install @ludovicm67/react-dotenv

Usage

Setup your project

Create a .env file at the root of your project, with some relevant values, like this:

API_URL=https://example.com
SOME_OTHER_VARIABLE=foo

Open your project's package.json file and add:

  • the react-dotenv command to your start and build scripts.
  • the react-dotenv.whitelist property to specify which variables you need to be exposed.

Here is an example:

package.json:

{
  // …other fields

  "scripts": {
    "start": "react-dotenv && react-scripts start", // <-- append command
    "build": "react-dotenv && react-scripts build", // <-- append command
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

  // …some other fields

  // Add the react-dotenv configuration
  "react-dotenv": {
    "whitelist": ["API_URL"]
  }
}

Access environment variables from your code

You can start the development server using the following command:

npm run start

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

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

Using the @ludovicm67/react-dotenv library

import React from "react";
import env from "@ludovicm67/react-dotenv";

const MyComponent = () => {
  return <div>{ env.API_URL }</div>;
};

export default MyComponent;

Using the window.env global variable

import React from "react";

const MyComponent = () => {
  return <div>{ window.env.API_URL }</div>;
};

export default MyComponent;

Known limitations

This only supports one environment (so only one .env file) and is not meant to do more.

Attributions

Forked from jeserodz/react-dotenv.

Reasons:

  • upgrade dependencies
  • use ESM
  • fix TypeScript types
  • fix the import of the env.js file in the index.html files