Skip to content

Commit

Permalink
Merge pull request #31 from flypapertech/add-node-cli-require-support
Browse files Browse the repository at this point in the history
Add node cli preload require support
  • Loading branch information
motdotla authored Feb 1, 2022
2 parents 1cc80d0 + b5a5e85 commit 94ba3aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ See [CONTRIBUTING.md](CONTRIBUTING.md)

See [CHANGELOG.md](CHANGELOG.md)

### Preload

You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#cli_r_require_module) to preload dotenv & dotenv-extend. By doing this, you do not need to require and load dotenv or dotenv-extend in your application code. This is the preferred approach when using `import` instead of `require`.

```bash
$ node -r dotenv-extend/config your_script.js
```

The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value`

```bash
$ node -r dotenv-extend/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-extend/config your_script.js
```

```bash
$ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv-extend/config your_script.js dotenv_config_path=/custom/path/to/.env
```

## Who's using dotenv-expand?

[These npm modules depend on it.](https://www.npmjs.com/browse/depended/dotenv-expand)
12 changes: 12 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(function () {
const dotenvExpand = require('./lib/main')
const env = require('dotenv').config(
Object.assign(
{},
require('dotenv/lib/env-options'),
require('dotenv/lib/cli-options')(process.argv)
)
)

return dotenvExpand(env)
})()

0 comments on commit 94ba3aa

Please sign in to comment.