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

dotenv-cra

v3.0.3

Published

Create React App style dotenv support for Node projects.

Downloads

14,543

Readme

build   coverage   npm

dotenv-cra

Create React App style dotenv support for Node projects. Combine a base .env file with a .env.${NODE_ENV} file to create your optimum configuration.

Note: It's not recommended that you store secrets (like private API keys) in your .env file(s). Secret configuration values should be managed and provided as part of your hosting solution.

Install

npm i dotenv-cra

Usage

Not much new here. As with dotenv, import/require dotenv-cra and configure it as early as possible. This ensures that any modules reading values from process.env can retrieve the expected values.

⚠️ Warning: The NODE_ENV variable must be set, so you may choose to default it in your application before calling config().

import { config } from 'dotenv-cra';

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
config();

Note When using NodeJS v15 or higher you can use Logical Nullish Assignment as well:

process.env.NODE_ENV ??= 'development';

At a minimum, create a base .env file in the root directory of your project with KEY=value entires on each line. However, if that's all you ever do, you don't need this library 😉. To see the real value of dotenv-cra, try creating a second .env.development file with some new and some overlapping KEY=value pairs.

# .env
LOG_LEVEL=info
PORT=3001

# .env.development
LOG_LEVEL=debug

# Loaded into process.env
LOG_LEVEL=debug
PORT=3001

What .env files can be used?

  • .env: Default.
  • .env.local: Local overrides. This file is loaded for all environments except test.
  • .env.development, .env.test, .env.production: Environment-specific settings.
  • .env.development.local, .env.test.local, .env.production.local: Local overrides of environment-specific settings.

Files on the left have more priority than files on the right:

  • npm start: .env.development.local, .env.local, .env.development, .env
  • npm test: .env.test.local, .env.test, .env (note .env.local is missing)

CRA Reference

Options

Env

Default: process.env.NODE_ENV

You may specify a custom environment if you don't want to base the .env.* files you load on NODE_ENV. For example, you may want NODE_ENV set to production, but you want to load the .env.staging file.

dotenvCra.config({ env: process.env.AWS_ENV });

Prefix

Default: none

You may specify a required prefix for your dotenv variables. For example, you may want to prefix your variables with WEB_API_ to ensure there aren't any collisions with other environment variables.

dotenvCra.config({ prefix: 'WEB_API_' });

Path

Default: path.resolve(process.cwd(), '.env')

You may specify a custom path if your file containing environment variables is located elsewhere. This will also be used as the basis for resolving the other .env.* files.

dotenvCra.config({ path: '/full/custom/path/to/your/.env' });

Encoding

Default: utf8

You may specify the encoding of your file containing environment variables. Passed through to dotenv.

dotenvCra.config({ encoding: 'latin1' });

Debug

Default: false

You may turn on logging to help debug why certain keys or values are not being set as you expect. Passed through to dotenv.

dotenvCra.config({ debug: process.env.DEBUG });

Credits

Thanks to these projects for this simple yet powerful approach 👏