dotenv
Advanced tools
Comparing version 12.0.0 to 12.0.1
@@ -5,6 +5,12 @@ # Changelog | ||
## [Unreleased](https://github.com/motdotla/dotenv/compare/v12.0.0...master) | ||
## [Unreleased](https://github.com/motdotla/dotenv/compare/v12.0.1...master) | ||
TBD | ||
## [12.0.1](https://github.com/motdotla/dotenv/compare/v12.0.0...v12.0.1) (2022-01-15) | ||
### Changed | ||
* README updates and clarifications | ||
## [12.0.0](https://github.com/motdotla/dotenv/compare/v11.0.0...v12.0.0) (2022-01-15) | ||
@@ -11,0 +17,0 @@ |
{ | ||
"name": "dotenv", | ||
"version": "12.0.0", | ||
"version": "12.0.1", | ||
"description": "Loads environment variables from .env file", | ||
@@ -20,3 +20,3 @@ "main": "lib/main.js", | ||
"lint": "standard", | ||
"postlint": "standard-markdown", | ||
"lint-readme": "standard-markdown", | ||
"pretest": "npm run lint && npm run dts-check", | ||
@@ -55,6 +55,3 @@ "test": "tap tests/*.js --100", | ||
"node": ">=12" | ||
}, | ||
"standard": { | ||
"ignore": [] | ||
} | ||
} |
150
README.md
@@ -23,22 +23,19 @@ <p align="center"> | ||
```bash | ||
# with npm | ||
npm install dotenv | ||
# or with Yarn | ||
yarn add dotenv | ||
# install locally (recommended) | ||
npm install dotenv --save | ||
``` | ||
Or installing with yarn? `yarn add dotenv` | ||
## Usage | ||
As early as possible in your application, require and configure dotenv. | ||
Usage is easy! | ||
```javascript | ||
require('dotenv').config() | ||
``` | ||
### 1. Create a `.env` file in the **root directory** of your project. | ||
Create a `.env` file in the root directory of your project. Add | ||
environment-specific variables on new lines in the form of `NAME=VALUE`. | ||
For example: | ||
```dosini | ||
# .env file | ||
# | ||
# Add environment-specific variables on new lines in the form of NAME=VALUE | ||
# | ||
DB_HOST=localhost | ||
@@ -49,5 +46,30 @@ DB_USER=root | ||
### 2. As early as possible in your application, import and configure dotenv. | ||
```javascript | ||
// index.js | ||
const dotenv = require('dotenv') | ||
dotenv.config() | ||
console.log(process.env) // remove this after you've confirmed it working | ||
``` | ||
.. or using typescript or esm? | ||
```javascript | ||
// index.mjs | ||
import dotenv from 'dotenv' | ||
dotenv.config() | ||
``` | ||
### 3. That's it! 🎉 | ||
`process.env` now has the keys and values you defined in your `.env` file. | ||
```javascript | ||
var dotenv = require('dotenv') | ||
dotenv.config() | ||
... | ||
const db = require('db') | ||
@@ -61,28 +83,24 @@ db.connect({ | ||
### Preload | ||
## Examples | ||
You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#cli_r_require_module) to preload dotenv. By doing this, you do not need to require and load dotenv in your application code. This is the preferred approach when using `import` instead of `require`. | ||
See [examples](https://github.com/dotenv-org/examples) of using dotenv with various frameworks, languages, and configurations. | ||
```bash | ||
$ node -r dotenv/config your_script.js | ||
``` | ||
* [nodejs](https://github.com/dotenv-org/examples/tree/master/dotenv-nodejs) | ||
* [nodejs (debug on)](https://github.com/dotenv-org/examples/tree/master/dotenv-nodejs-debug) | ||
* [esm](https://github.com/dotenv-org/examples/tree/master/dotenv-esm) | ||
* [esm (preload)](https://github.com/dotenv-org/examples/tree/master/dotenv-esm-preload) | ||
* [typescript](https://github.com/dotenv-org/examples/tree/master/dotenv-typescript) | ||
* [webpack](https://github.com/dotenv-org/examples/tree/master/dotenv-webpack) | ||
* [react](https://github.com/dotenv-org/examples/tree/master/dotenv-react) | ||
* [react (typescript)](https://github.com/dotenv-org/examples/tree/master/dotenv-react-typescript) | ||
The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value` | ||
## Available Functions | ||
```bash | ||
$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env | ||
``` | ||
Dotenv exposes two functions: | ||
Additionally, you can use environment variables to set configuration options. Command line arguments will precede these. | ||
* `dotenv.config` | ||
* `dotenv.parse` | ||
```bash | ||
$ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js | ||
``` | ||
### `dotenv.config` | ||
```bash | ||
$ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env | ||
``` | ||
## Config | ||
`config` will read your `.env` file, parse the contents, assign it to | ||
@@ -104,5 +122,5 @@ [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env), | ||
### Options | ||
#### Options | ||
#### Path | ||
##### Path | ||
@@ -117,3 +135,3 @@ Default: `path.resolve(process.cwd(), '.env')` | ||
#### Encoding | ||
##### Encoding | ||
@@ -128,3 +146,3 @@ Default: `utf8` | ||
#### Debug | ||
##### Debug | ||
@@ -139,3 +157,3 @@ Default: `false` | ||
## Parse | ||
### `dotenv.parse` | ||
@@ -153,5 +171,5 @@ The engine which parses the contents of your file containing environment | ||
### Options | ||
#### Options | ||
#### Debug | ||
##### Debug | ||
@@ -170,21 +188,28 @@ Default: `false` | ||
### Rules | ||
## Other Usage | ||
The parsing engine currently supports the following rules: | ||
### Preload | ||
- `BASIC=basic` becomes `{BASIC: 'basic'}` | ||
- empty lines are skipped | ||
- lines beginning with `#` are treated as comments | ||
- empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`) | ||
- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`) | ||
- whitespace is removed from both ends of unquoted values (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO= some value ` becomes `{FOO: 'some value'}`) | ||
- single and double quoted values are escaped (`SINGLE_QUOTE='quoted'` becomes `{SINGLE_QUOTE: "quoted"}`) | ||
- single and double quoted values maintain whitespace from both ends (`FOO=" some value "` becomes `{FOO: ' some value '}`) | ||
- double quoted values expand new lines (`MULTILINE="new\nline"` becomes | ||
You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#cli_r_require_module) to preload dotenv. By doing this, you do not need to require and load dotenv in your application code. This is the preferred approach when using `import` instead of `require`. | ||
```bash | ||
$ node -r dotenv/config your_script.js | ||
``` | ||
{MULTILINE: 'new | ||
line'} | ||
The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value` | ||
```bash | ||
$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env | ||
``` | ||
Additionally, you can use environment variables to set configuration options. Command line arguments will precede these. | ||
```bash | ||
$ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js | ||
``` | ||
```bash | ||
$ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env | ||
``` | ||
## FAQ | ||
@@ -207,2 +232,21 @@ | ||
### What rules does the parsing engine follow? | ||
The parsing engine currently supports the following rules: | ||
- `BASIC=basic` becomes `{BASIC: 'basic'}` | ||
- empty lines are skipped | ||
- lines beginning with `#` are treated as comments | ||
- empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`) | ||
- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`) | ||
- whitespace is removed from both ends of unquoted values (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO= some value ` becomes `{FOO: 'some value'}`) | ||
- single and double quoted values are escaped (`SINGLE_QUOTE='quoted'` becomes `{SINGLE_QUOTE: "quoted"}`) | ||
- single and double quoted values maintain whitespace from both ends (`FOO=" some value "` becomes `{FOO: ' some value '}`) | ||
- double quoted values expand new lines (`MULTILINE="new\nline"` becomes | ||
``` | ||
{MULTILINE: 'new | ||
line'} | ||
``` | ||
### What happens to environment variables that were already set? | ||
@@ -278,3 +322,3 @@ | ||
## Change Log | ||
## CHANGELOG | ||
@@ -281,0 +325,0 @@ See [CHANGELOG.md](CHANGELOG.md) |
25808
321