nav tabs on admin dashboard
This commit is contained in:
+108
-21
@@ -5,6 +5,7 @@
|
||||
Dotenv 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.
|
||||
|
||||
[](https://travis-ci.org/motdotla/dotenv)
|
||||
[](https://ci.appveyor.com/project/motdotla/dotenv/branch/master)
|
||||
[](https://www.npmjs.com/package/dotenv)
|
||||
[](https://github.com/feross/standard)
|
||||
[](https://coveralls.io/github/motdotla/dotenv?branch=coverall-intergration)
|
||||
@@ -12,7 +13,11 @@ Dotenv is a zero-dependency module that loads environment variables from a `.env
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install dotenv --save
|
||||
# with npm
|
||||
npm install dotenv
|
||||
|
||||
# or with Yarn
|
||||
yarn add dotenv
|
||||
```
|
||||
|
||||
## Usage
|
||||
@@ -27,7 +32,7 @@ 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
|
||||
DB_HOST=localhost
|
||||
DB_USER=root
|
||||
DB_PASS=s1mpl3
|
||||
@@ -38,7 +43,7 @@ That's it.
|
||||
`process.env` now has the keys and values you defined in your `.env` file.
|
||||
|
||||
```javascript
|
||||
var db = require('db')
|
||||
const db = require('db')
|
||||
db.connect({
|
||||
host: process.env.DB_HOST,
|
||||
username: process.env.DB_USER,
|
||||
@@ -48,8 +53,7 @@ db.connect({
|
||||
|
||||
### Preload
|
||||
|
||||
If you are using iojs-v1.6.0 or later, you can use the `--require` (`-r`) command line option to preload dotenv. By doing this, you do not need to require and load dotenv in your application code.
|
||||
|
||||
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
|
||||
@@ -61,37 +65,66 @@ The configuration options below are supported as command line arguments in the f
|
||||
$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars
|
||||
```
|
||||
|
||||
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=base64 node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
|
||||
```
|
||||
|
||||
## Config
|
||||
|
||||
_Alias: `load`_
|
||||
|
||||
`config` will read your .env file, parse the contents, assign it to
|
||||
[`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env),
|
||||
and return an Object with a _parsed_ key containing the loaded content or an _error_ key if it failed.
|
||||
and return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.
|
||||
|
||||
```js
|
||||
const result = dotenv.config()
|
||||
|
||||
if (result.error) {
|
||||
throw result.error
|
||||
}
|
||||
|
||||
console.log(result.parsed)
|
||||
```
|
||||
|
||||
You can additionally, pass options to `config`.
|
||||
|
||||
### Options
|
||||
|
||||
#### Path
|
||||
|
||||
Default: `.env`
|
||||
Default: `path.resolve(process.cwd(), '.env')`
|
||||
|
||||
You can specify a custom path if your file containing environment variables is
|
||||
named or located differently.
|
||||
You may specify a custom path if your file containing environment variables is located elsewhere.
|
||||
|
||||
```js
|
||||
require('dotenv').config({path: '/custom/path/to/your/env/vars'})
|
||||
require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })
|
||||
```
|
||||
|
||||
#### Encoding
|
||||
|
||||
Default: `utf8`
|
||||
|
||||
You may specify the encoding of your file containing environment variables
|
||||
using this option.
|
||||
You may specify the encoding of your file containing environment variables.
|
||||
|
||||
```js
|
||||
require('dotenv').config({encoding: 'base64'})
|
||||
require('dotenv').config({ encoding: 'base64' })
|
||||
```
|
||||
|
||||
#### Debug
|
||||
|
||||
Default: `false`
|
||||
|
||||
You may turn on logging to help debug why certain keys or values are not being set as you expect.
|
||||
|
||||
```js
|
||||
require('dotenv').config({ debug: process.env.DEBUG })
|
||||
```
|
||||
|
||||
## Parse
|
||||
@@ -101,12 +134,28 @@ variables is available to use. It accepts a String or Buffer and will return
|
||||
an Object with the parsed keys and values.
|
||||
|
||||
```js
|
||||
var dotenv = require('dotenv')
|
||||
var buf = new Buffer('BASIC=basic')
|
||||
var config = dotenv.parse(buf) // will return an object
|
||||
const dotenv = require('dotenv')
|
||||
const buf = Buffer.from('BASIC=basic')
|
||||
const config = dotenv.parse(buf) // will return an object
|
||||
console.log(typeof config, config) // object { BASIC : 'basic' }
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
#### Debug
|
||||
|
||||
Default: `false`
|
||||
|
||||
You may turn on logging to help debug why certain keys or values are not being set as you expect.
|
||||
|
||||
```js
|
||||
const dotenv = require('dotenv')
|
||||
const buf = Buffer.from('hello world')
|
||||
const opt = { debug: true }
|
||||
const config = dotenv.parse(buf, opt)
|
||||
// expect a debug message because the buffer is not in KEY=VAL form
|
||||
```
|
||||
|
||||
### Rules
|
||||
|
||||
The parsing engine currently supports the following rules:
|
||||
@@ -122,7 +171,9 @@ The parsing engine currently supports the following rules:
|
||||
{MULTILINE: 'new
|
||||
line'}
|
||||
```
|
||||
|
||||
- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
|
||||
- whitespace is removed from both ends of the value (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'}`)
|
||||
|
||||
## FAQ
|
||||
|
||||
@@ -151,7 +202,7 @@ If you want to override `process.env` you can do something like this:
|
||||
const fs = require('fs')
|
||||
const dotenv = require('dotenv')
|
||||
const envConfig = dotenv.parse(fs.readFileSync('.env.override'))
|
||||
for (var k in envConfig) {
|
||||
for (let k in envConfig) {
|
||||
process.env[k] = envConfig[k]
|
||||
}
|
||||
```
|
||||
@@ -163,17 +214,49 @@ the parsed `.env` file. This gives you everything you need to continue
|
||||
setting values on `process.env`. For example:
|
||||
|
||||
```js
|
||||
var dotenv = require('dotenv')
|
||||
var variableExpansion = require('dotenv-expand')
|
||||
const dotenv = require('dotenv')
|
||||
const variableExpansion = require('dotenv-expand')
|
||||
const myEnv = dotenv.config()
|
||||
variableExpansion(myEnv)
|
||||
```
|
||||
|
||||
### What about variable expansion?
|
||||
|
||||
For `dotenv@2.x.x`: Use [dotenv-expand](https://github.com/motdotla/dotenv-expand).
|
||||
Try [dotenv-expand](https://github.com/motdotla/dotenv-expand)
|
||||
|
||||
For `dotenv@1.x.x`: We haven't been presented with a compelling use case for expanding variables and believe it leads to env vars that are not "fully orthogonal" as [The Twelve-Factor App](http://12factor.net/config) outlines.<sup>[[1](https://github.com/motdotla/dotenv/issues/39)][[2](https://github.com/motdotla/dotenv/pull/97)]</sup> Please open an issue if you have a compelling use case.
|
||||
### How do I use dotenv with `import`?
|
||||
|
||||
ES2015 and beyond offers modules that allow you to `export` any top-level `function`, `class`, `var`, `let`, or `const`.
|
||||
|
||||
> 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.
|
||||
>
|
||||
> – [ES6 In Depth: Modules](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/)
|
||||
|
||||
You must run `dotenv.config()` before referencing any environment variables. Here's an example of problematic code:
|
||||
|
||||
`errorReporter.js`:
|
||||
|
||||
```js
|
||||
import { Client } from 'best-error-reporting-service'
|
||||
|
||||
export const client = new Client(process.env.BEST_API_KEY)
|
||||
```
|
||||
|
||||
`index.js`:
|
||||
|
||||
```js
|
||||
import dotenv from 'dotenv'
|
||||
import errorReporter from './errorReporter'
|
||||
|
||||
dotenv.config()
|
||||
errorReporter.client.report(new Error('faq example'))
|
||||
```
|
||||
|
||||
`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.
|
||||
|
||||
1. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need to `import` dotenv with this approach_)
|
||||
2. Import `dotenv/config` instead of `dotenv` (_Note: you do not need to call `dotenv.config()` and must pass options via the command line with this approach_)
|
||||
3. 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)
|
||||
|
||||
## Contributing Guide
|
||||
|
||||
@@ -206,3 +289,7 @@ Here's some projects that expand on dotenv. Check them out.
|
||||
* [require-environment-variables](https://github.com/bjoshuanoah/require-environment-variables)
|
||||
* [dotenv-safe](https://github.com/rolodato/dotenv-safe)
|
||||
* [envalid](https://github.com/af/envalid)
|
||||
* [lookenv](https://github.com/RodrigoEspinosa/lookenv)
|
||||
* [run.env](https://www.npmjs.com/package/run.env)
|
||||
* [dotenv-webpack](https://github.com/mrsteele/dotenv-webpack)
|
||||
* [env-path](https://github.com/benrei/env-path)
|
||||
|
||||
Reference in New Issue
Block a user