Skip to content

Commit 953702a

Browse files
authored
Merge pull request #608 from motdotla/branch-15
Moving toward a cleaner implementation
2 parents 97f32c1 + a487795 commit 953702a

12 files changed

+157
-270
lines changed

CHANGELOG.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,29 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5-
## [Unreleased](https://github.com/motdotla/dotenv/compare/v14.3.2...master)
5+
## [Unreleased](https://github.com/motdotla/dotenv/compare/v15.0.0...master)
6+
7+
## [15.0.0](https://github.com/motdotla/dotenv/compare/v14.3.2...v15.0.0) (2022-01-26)
8+
9+
`v15.0.0` is a major new release with some important breaking changes.
10+
11+
### Added
12+
13+
- _Breaking:_ Multiline parsing support (just works. no need for the flag.)
14+
15+
### Changed
16+
17+
- _Breaking:_ `#` marks the beginning of a comment (UNLESS the value is wrapped in quotes. Please update your `.env` files to wrap in quotes any values containing `#`. For example: `SECRET_HASH="something-with-a-#-hash"`).
18+
19+
..Understandably, (as some teams have noted) this is tedious to do across the entire team. To make it less tedious, we recommend using [dotenv cli](https://github.com/dotenv-org/cli) going forward. It's an optional plugin that will keep your `.env` files in sync between machines, environments, or team members.
20+
21+
### Removed
22+
23+
- _Breaking:_ Remove multiline option (just works out of the box now. no need for the flag.)
24+
25+
### Changed
26+
27+
- Preserve backwards compatibility on values containing `#` 🐞 ([#603](https://github.com/motdotla/dotenv/pull/603))
628

729
## [14.3.2](https://github.com/motdotla/dotenv/compare/v14.3.1...v14.3.2) (2022-01-25)
830

README.md

+81-89
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,110 @@ Or installing with yarn? `yarn add dotenv`
2828

2929
## Usage
3030

31-
Usage is easy!
32-
33-
### 1. Create a `.env` file in the **root directory** of your project.
31+
Create a `.env` file in the root of your project:
3432

3533
```dosini
36-
# .env file
37-
#
38-
# Add environment-specific variables on new lines in the form of NAME=VALUE
39-
#
40-
DB_HOST=localhost
41-
DB_USER=root
42-
DB_PASS=s1mpl3
34+
S3_BUCKET="YOURS3BUCKET"
35+
SECRET_KEY="YOURSECRETKEYGOESHERE"
4336
```
4437

45-
### 2. As early as possible in your application, import and configure dotenv.
38+
As early as possible in your application, import and configure dotenv:
4639

4740
```javascript
48-
// index.js
4941
require('dotenv').config()
50-
5142
console.log(process.env) // remove this after you've confirmed it working
5243
```
5344

5445
.. or using ES6?
5546

5647
```javascript
57-
// index.mjs (ESM)
5848
import 'dotenv/config' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
5949
import express from 'express'
6050
```
6151

62-
### 3. That's it! 🎉
63-
64-
`process.env` now has the keys and values you defined in your `.env` file.
52+
That's it. `process.env` now has the keys and values you defined in your `.env` file:
6553

6654
```javascript
6755
require('dotenv').config()
6856

6957
...
7058

71-
const db = require('db')
72-
db.connect({
73-
host: process.env.DB_HOST,
74-
username: process.env.DB_USER,
75-
password: process.env.DB_PASS
76-
})
59+
s3.getBucketCors({Bucket: process.env.S3_BUCKET}, function(err, data) {})
60+
```
61+
62+
### Multiline values
63+
64+
If you need multiline variables, for example private keys, those are now supported (`>= v15.0.0`) with line breaks:
65+
66+
```dosini
67+
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
68+
...
69+
Kh9NV...
70+
...
71+
-----END DSA PRIVATE KEY-----"
72+
```
73+
74+
Alternatively, you can double quote strings and use the `\n` character:
75+
76+
```dosini
77+
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\Kh9NV...\n-----END DSA PRIVATE KEY-----\n"
78+
```
79+
80+
### Comments
81+
82+
Comments may be added to your file on their own line or inline:
83+
84+
```dosini
85+
# This is a comment
86+
SECRET_KEY=YOURSECRETKEYGOESHERE # comment
87+
SECRET_HASH="something-with-a-#-hash"
88+
```
89+
90+
Comments begin where a `#` exists, so if your value contains a `#` please wrap it in quotes. This is a breaking change from `>= v15.0.0` and on.
91+
92+
### Parsing
93+
94+
The engine which parses the contents of your file containing environment variables is available to use. It accepts a String or Buffer and will return an Object with the parsed keys and values.
95+
96+
```javascript
97+
const dotenv = require('dotenv')
98+
const buf = Buffer.from('BASIC=basic')
99+
const config = dotenv.parse(buf) // will return an object
100+
console.log(typeof config, config) // object { BASIC : 'basic' }
101+
```
102+
103+
### Preload
104+
105+
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.
106+
107+
```bash
108+
$ node -r dotenv/config your_script.js
109+
```
110+
111+
The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value`
112+
113+
```bash
114+
$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env dotenv_config_debug=true
77115
```
78116

117+
Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.
118+
119+
```bash
120+
$ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js
121+
```
122+
123+
```bash
124+
$ DOTENV_CONFIG_ENCODING=latin1 DOTENV_CONFIG_DEBUG=true node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
125+
```
126+
127+
### Variable Expansion
128+
129+
You need to add the value of another variable in one of your variables? Use [dotenv-expand](https://github.com/motdotla/dotenv-expand).
130+
131+
### Syncing
132+
133+
You need to keep `.env` files in sync between machines, environments, or team members? Use [dotenv cli](https://github.com/dotenv-org/cli).
134+
79135
## Examples
80136

81137
See [examples](https://github.com/dotenv-org/examples) of using dotenv with various frameworks, languages, and configurations.
@@ -163,27 +219,6 @@ Override any environment variables that have already been set on your machine wi
163219
require('dotenv').config({ override: true })
164220
```
165221

166-
##### Multiline
167-
168-
Default: `false`
169-
170-
Turn on multiline line break parsing.
171-
172-
```js
173-
require('dotenv').config({ multiline: true })
174-
```
175-
176-
This allows specifying multiline values in this format:
177-
178-
```
179-
PRIVATE_KEY="-----BEGIN PRIVATE KEY-----
180-
MIGT...
181-
7ure...
182-
-----END PRIVATE KEY-----"
183-
```
184-
185-
Ensure that the value begins with a single or double quote character, and it ends with the same character.
186-
187222
### Parse
188223

189224
The engine which parses the contents of your file containing environment
@@ -213,51 +248,8 @@ const config = dotenv.parse(buf, opt)
213248
// expect a debug message because the buffer is not in KEY=VAL form
214249
```
215250

216-
##### Multiline
217-
218-
Default: `false`
219-
220-
Turn on multiline line break parsing.
221-
222-
```js
223-
require('dotenv').config({ multiline: true })
224-
```
225-
226-
This allows specifying multiline values in this format:
227-
228-
```
229-
PRIVATE_KEY="-----BEGIN PRIVATE KEY-----
230-
MIGT...
231-
7ure...
232-
-----END PRIVATE KEY-----"
233-
```
234-
235251
## Other Usage
236252

237-
### Preload
238-
239-
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`.
240-
241-
```bash
242-
$ node -r dotenv/config your_script.js
243-
```
244-
245-
The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value`
246-
247-
```bash
248-
$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env dotenv_config_debug=true
249-
```
250-
251-
Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.
252-
253-
```bash
254-
$ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js
255-
```
256-
257-
```bash
258-
$ DOTENV_CONFIG_ENCODING=latin1 DOTENV_CONFIG_DEBUG=true node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
259-
```
260-
261253
## FAQ
262254

263255
### Why is the `.env` file not loading my environment variables successfully?
@@ -294,7 +286,7 @@ The parsing engine currently supports the following rules:
294286
- `BASIC=basic` becomes `{BASIC: 'basic'}`
295287
- empty lines are skipped
296288
- lines beginning with `#` are treated as comments
297-
- whitespace followed by `#` marks the beginning of an inline comment (unless when the value is wrapped in quotes)
289+
- `#` marks the beginning of a comment (unless when the value is wrapped in quotes)
298290
- empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`)
299291
- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
300292
- 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'}`)
@@ -383,7 +375,7 @@ errorReporter.report(new Error('documented example'))
383375

384376
Does that make sense? It's a bit unintuitive, but it is how importing of ES6 modules work. Here is a [working example of this pitfall](https://github.com/dotenv-org/examples/tree/master/dotenv-es6-import-pitfall).
385377

386-
There are also 2 alternatives to this approach:
378+
There are two alternatives to this approach:
387379

388380
1. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need to `import` dotenv with this approach_)
389381
2. Create a separate file that will execute `config` first as outlined in [this comment on #133](https://github.com/motdotla/dotenv/issues/133#issuecomment-255298822)

lib/main.d.ts

+1-28
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,6 @@
11
// TypeScript Version: 3.0
22
/// <reference types="node" />
33

4-
export interface DotenvParseOptions {
5-
/**
6-
* Default: `false`
7-
*
8-
* Turn on logging to help debug why certain keys or values are not being set as you expect.
9-
*
10-
* example: `dotenv.parse('KEY=value', { debug: true })`
11-
*/
12-
debug?: boolean;
13-
14-
/**
15-
* Default: `false`
16-
*
17-
* Turn on multiline line break parsing.
18-
*
19-
* example:
20-
*
21-
* MY_VAR="this
22-
* is
23-
* a
24-
* multiline
25-
* string"
26-
*/
27-
multiline?: boolean;
28-
}
29-
304
export interface DotenvParseOutput {
315
[name: string]: string;
326
}
@@ -41,8 +15,7 @@ export interface DotenvParseOutput {
4115
* @returns an object with keys and values based on `src`. example: `{ DB_HOST : 'localhost' }`
4216
*/
4317
export function parse<T extends DotenvParseOutput = DotenvParseOutput>(
44-
src: string | Buffer,
45-
options?: DotenvParseOptions
18+
src: string | Buffer
4619
): T;
4720

4821
export interface DotenvConfigOptions {

0 commit comments

Comments
 (0)