# dotenv-cra

> Create React App style dotenv support for Node projects.

Latest version **3.0.3** (published 2023-01-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install dotenv-cra
pnpm add dotenv-cra
yarn add dotenv-cra
bun add dotenv-cra
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.3 |
| Published | 2023-01-20 |
| First published | 2019-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 2 |
| Unpacked size | 9.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Dan Dombrowski |
| Maintainers | djdmbrwsk |
| Keywords | dotenv, env, .env, environment, variables, config, settings, dotenv-expand, expand, cra, create-react-app, node, node.js |

## Links

- npm: https://www.npmjs.com/package/dotenv-cra
- Repository: https://github.com/djdmbrwsk/dotenv-cra
- Homepage: https://github.com/djdmbrwsk/dotenv-cra#readme
- Issues: https://github.com/djdmbrwsk/dotenv-cra/issues
- npm.io page: https://npm.io/package/dotenv-cra

## Dependencies (2)

- [dotenv](https://npm.io/package/dotenv.md) ^10.0.0
- [dotenv-expand](https://npm.io/package/dotenv-expand.md) ^5.1.0

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 3.0.3 (latest) — 2023-01-20
- 2.0.0-beta.2 (beta) — 2020-09-03
- 3.0.2 — 2021-09-01
- 3.0.1 — 2021-07-05
- 3.0.0 — 2021-07-04
- 2.2.0 — 2021-07-04
- 2.1.0 — 2021-06-19
- 2.0.2 — 2020-10-22
- 2.0.1 — 2020-09-08
- 2.0.0 — 2020-09-03
- 2.0.0-beta.1 — 2020-09-03
- 1.0.0 — 2019-12-29
- 0.0.6 — 2019-12-15
- 0.0.5 — 2019-12-14
- 0.0.4 — 2019-12-14
- … 4 more at https://npm.io/package/dotenv-cra/versions

## README

[![build](https://img.shields.io/github/actions/workflow/status/djdmbrwsk/dotenv-cra/lint-build-test.yml?branch=master)](https://github.com/djdmbrwsk/dotenv-cra/actions?query=branch%3Amaster+workflow%3A%22Lint%2C+build%2C+test%22)
&nbsp;
[![coverage](https://img.shields.io/codecov/c/gh/djdmbrwsk/dotenv-cra)](https://codecov.io/gh/djdmbrwsk/dotenv-cra/branch/master)
&nbsp;
[![npm](https://img.shields.io/npm/v/dotenv-cra)](https://www.npmjs.com/package/dotenv-cra)

# dotenv-cra

Create React App style [dotenv](https://github.com/motdotla/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()`.

```ts
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment) as well:

```ts
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](https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used)

## 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.

```ts
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.

```ts
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.

```ts
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.**

```ts
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.**

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

## Credits

Thanks to these projects for this simple yet powerful approach 👏

- [dotenv](https://github.com/motdotla/dotenv)
- [dotenv-expand](https://github.com/motdotla/dotenv-expand)
- [create-react-app](https://github.com/facebook/create-react-app)

---
_Source: https://npm.io/package/dotenv-cra · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
