source: frontend/node_modules/dotenv/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 9.8 KB
Line 
1<p align="center">
2<strong>Announcement 📣</strong><br/>From the makers that brought you Dotenv, introducing <a href="https://sync.dotenv.org">Dotenv Sync</a>.<br/>Sync your .env files between machines, environments, and team members.<br/><a href="https://sync.dotenv.org">Join the early access list. 🕶</a>
3</p>
4
5# dotenv
6
7<img src="https://raw.githubusercontent.com/motdotla/dotenv/master/dotenv.png" alt="dotenv" align="right" />
8
9Dotenv is a zero-dependency module that loads environment variables from a `.env` file into [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env). Storing configuration in the environment separate from code is based on [The Twelve-Factor App](http://12factor.net/config) methodology.
10
11[![BuildStatus](https://img.shields.io/travis/motdotla/dotenv/master.svg?style=flat-square)](https://travis-ci.org/motdotla/dotenv)
12[![Build status](https://ci.appveyor.com/api/projects/status/github/motdotla/dotenv?svg=true)](https://ci.appveyor.com/project/motdotla/dotenv/branch/master)
13[![NPM version](https://img.shields.io/npm/v/dotenv.svg?style=flat-square)](https://www.npmjs.com/package/dotenv)
14[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
15[![Coverage Status](https://img.shields.io/coveralls/motdotla/dotenv/master.svg?style=flat-square)](https://coveralls.io/github/motdotla/dotenv?branch=coverall-intergration)
16[![LICENSE](https://img.shields.io/github/license/motdotla/dotenv.svg)](LICENSE)
17[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
18
19## Install
20
21```bash
22# with npm
23npm install dotenv
24
25# or with Yarn
26yarn add dotenv
27```
28
29## Usage
30
31As early as possible in your application, require and configure dotenv.
32
33```javascript
34require('dotenv').config()
35```
36
37Create a `.env` file in the root directory of your project. Add
38environment-specific variables on new lines in the form of `NAME=VALUE`.
39For example:
40
41```dosini
42DB_HOST=localhost
43DB_USER=root
44DB_PASS=s1mpl3
45```
46
47`process.env` now has the keys and values you defined in your `.env` file.
48
49```javascript
50const db = require('db')
51db.connect({
52 host: process.env.DB_HOST,
53 username: process.env.DB_USER,
54 password: process.env.DB_PASS
55})
56```
57
58### Preload
59
60You 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`.
61
62```bash
63$ node -r dotenv/config your_script.js
64```
65
66The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value`
67
68```bash
69$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
70```
71
72Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.
73
74```bash
75$ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js
76```
77
78```bash
79$ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
80```
81
82## Config
83
84`config` will read your `.env` file, parse the contents, assign it to
85[`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env),
86and return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.
87
88```js
89const result = dotenv.config()
90
91if (result.error) {
92 throw result.error
93}
94
95console.log(result.parsed)
96```
97
98You can additionally, pass options to `config`.
99
100### Options
101
102#### Path
103
104Default: `path.resolve(process.cwd(), '.env')`
105
106You may specify a custom path if your file containing environment variables is located elsewhere.
107
108```js
109require('dotenv').config({ path: '/custom/path/to/.env' })
110```
111
112#### Encoding
113
114Default: `utf8`
115
116You may specify the encoding of your file containing environment variables.
117
118```js
119require('dotenv').config({ encoding: 'latin1' })
120```
121
122#### Debug
123
124Default: `false`
125
126You may turn on logging to help debug why certain keys or values are not being set as you expect.
127
128```js
129require('dotenv').config({ debug: process.env.DEBUG })
130```
131
132## Parse
133
134The engine which parses the contents of your file containing environment
135variables is available to use. It accepts a String or Buffer and will return
136an Object with the parsed keys and values.
137
138```js
139const dotenv = require('dotenv')
140const buf = Buffer.from('BASIC=basic')
141const config = dotenv.parse(buf) // will return an object
142console.log(typeof config, config) // object { BASIC : 'basic' }
143```
144
145### Options
146
147#### Debug
148
149Default: `false`
150
151You may turn on logging to help debug why certain keys or values are not being set as you expect.
152
153```js
154const dotenv = require('dotenv')
155const buf = Buffer.from('hello world')
156const opt = { debug: true }
157const config = dotenv.parse(buf, opt)
158// expect a debug message because the buffer is not in KEY=VAL form
159```
160
161### Rules
162
163The parsing engine currently supports the following rules:
164
165- `BASIC=basic` becomes `{BASIC: 'basic'}`
166- empty lines are skipped
167- lines beginning with `#` are treated as comments
168- empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`)
169- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
170- 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'}`)
171- single and double quoted values are escaped (`SINGLE_QUOTE='quoted'` becomes `{SINGLE_QUOTE: "quoted"}`)
172- single and double quoted values maintain whitespace from both ends (`FOO=" some value "` becomes `{FOO: ' some value '}`)
173- double quoted values expand new lines (`MULTILINE="new\nline"` becomes
174
175```
176{MULTILINE: 'new
177line'}
178```
179
180## FAQ
181
182### Should I commit my `.env` file?
183
184No. We **strongly** recommend against committing your `.env` file to version
185control. It should only include environment-specific values such as database
186passwords or API keys. Your production database should have a different
187password than your development database.
188
189### Should I have multiple `.env` files?
190
191No. We **strongly** recommend against having a "main" `.env` file and an "environment" `.env` file like `.env.test`. Your config should vary between deploys, and you should not be sharing values between environments.
192
193> In a twelve-factor app, env vars are granular controls, each fully orthogonal to other env vars. They are never grouped together as “environments”, but instead are independently managed for each deploy. This is a model that scales up smoothly as the app naturally expands into more deploys over its lifetime.
194>
195> – [The Twelve-Factor App](http://12factor.net/config)
196
197### What happens to environment variables that were already set?
198
199We will never modify any environment variables that have already been set. In particular, if there is a variable in your `.env` file which collides with one that already exists in your environment, then that variable will be skipped. This behavior allows you to override all `.env` configurations with a machine-specific environment, although it is not recommended.
200
201If you want to override `process.env` you can do something like this:
202
203```javascript
204const fs = require('fs')
205const dotenv = require('dotenv')
206const envConfig = dotenv.parse(fs.readFileSync('.env.override'))
207for (const k in envConfig) {
208 process.env[k] = envConfig[k]
209}
210```
211
212### Can I customize/write plugins for dotenv?
213
214For `dotenv@2.x.x`: Yes. `dotenv.config()` now returns an object representing
215the parsed `.env` file. This gives you everything you need to continue
216setting values on `process.env`. For example:
217
218```js
219const dotenv = require('dotenv')
220const variableExpansion = require('dotenv-expand')
221const myEnv = dotenv.config()
222variableExpansion(myEnv)
223```
224
225### What about variable expansion?
226
227Try [dotenv-expand](https://github.com/motdotla/dotenv-expand)
228
229### How do I use dotenv with `import`?
230
231ES2015 and beyond offers modules that allow you to `export` any top-level `function`, `class`, `var`, `let`, or `const`.
232
233> When you run a module containing an `import` declaration, the modules it imports are loaded first, then each module body is executed in a depth-first traversal of the dependency graph, avoiding cycles by skipping anything already executed.
234>
235> – [ES6 In Depth: Modules](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/)
236
237You must run `dotenv.config()` before referencing any environment variables. Here's an example of problematic code:
238
239`errorReporter.js`:
240
241```js
242import { Client } from 'best-error-reporting-service'
243
244export const client = new Client(process.env.BEST_API_KEY)
245```
246
247`index.js`:
248
249```js
250import dotenv from 'dotenv'
251import errorReporter from './errorReporter'
252
253dotenv.config()
254errorReporter.client.report(new Error('faq example'))
255```
256
257`client` will not be configured correctly because it was constructed before `dotenv.config()` was executed. There are (at least) 3 ways to make this work.
258
2591. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need to `import` dotenv with this approach_)
2602. Import `dotenv/config` instead of `dotenv` (_Note: you do not need to call `dotenv.config()` and must pass options via the command line or environment variables with this approach_)
2613. 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)
262
263## Contributing Guide
264
265See [CONTRIBUTING.md](CONTRIBUTING.md)
266
267## Change Log
268
269See [CHANGELOG.md](CHANGELOG.md)
270
271## Who's using dotenv?
272
273[These npm modules depend on it.](https://www.npmjs.com/browse/depended/dotenv)
274
275Projects that expand it often use the [keyword "dotenv" on npm](https://www.npmjs.com/search?q=keywords:dotenv).
Note: See TracBrowser for help on using the repository browser.