|
| 1 | +<h1>@dotenv-run/cli</h1> |
| 2 | + |
| 3 | +A CLI tool to load command line and .env environment variables with monorepo support. |
| 4 | + |
| 5 | +- ✅ Loading environment variables from the command line `API_BASE=/v1/ dotenv-run -- npm start` |
| 6 | +- ✅ Load environment variables from `.env` files |
| 7 | +- ✅ Expand environment variables `API_URL=$API_BASE/users` |
| 8 | +- ✅ Define environment variables for a specific (e.g. `.env.production`) |
| 9 | +- ✅ Load priorities of `.env.*` files (e.g. `.env.production` > `.env`) |
| 10 | +- ✅ Supports hierarchical cascading configuration in monorepo projects ([Nx](https://nx.dev), [Turbo](https://turbo.build/), etc.) |
| 11 | + `apps/next-app/.env` > `apps/.env` > `.env` |
| 12 | +- ✅ Supports all platforms and languages (Node.js, Python...) `dotenv-run -- python main.py` |
| 13 | + |
| 14 | +# Quick Start |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```sh |
| 19 | +npm add -D @dotenv-run/cli |
| 20 | +``` |
| 21 | + |
| 22 | +## Prompt |
| 23 | + |
| 24 | +```sh |
| 25 | +$> dotenv-run -h |
| 26 | + |
| 27 | +Usage: dotenv-run [options] -- <command> |
| 28 | + |
| 29 | +Options: |
| 30 | + |
| 31 | + -h, --help output usage information |
| 32 | + -e, --env environment name (e.g. dev, prod) |
| 33 | + -s, --silent do not print .env file paths |
| 34 | + -r, --root root directory to search for .env files |
| 35 | + |
| 36 | +Examples: |
| 37 | + |
| 38 | + dotenv-run -- npm start |
| 39 | + dotenv-run -r ../.. -- npm start |
| 40 | + dotenv-run -e prod -- npm start |
| 41 | +``` |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +In addition to loading environment variables, **@dotenv-run/cli** supports monorepo projects with multiple applications. |
| 46 | + |
| 47 | +In a monorepo configuration, `.env.*` files can be defined in the root workspace and overriden by each application. |
| 48 | + |
| 49 | +**Root workspace** |
| 50 | + |
| 51 | +`dotenv-run` will search and load `.env.*` files located in the root workspace down to the current working directory. |
| 52 | + |
| 53 | +If no root workspace is found, `dotenv-run` will load environment files within the current working directory. |
| 54 | + |
| 55 | +You can specify a root workspace with the `-r` option. |
| 56 | + |
| 57 | +**Example** |
| 58 | + |
| 59 | +Given the following files: |
| 60 | + |
| 61 | +```sh |
| 62 | +platform |
| 63 | +├── apps |
| 64 | +│ ├── frontend1 |
| 65 | +│ │ ├── .env.local # API_USERS=http://localhost:3001/users |
| 66 | +│ │ └── vite.config.js |
| 67 | +│ └── frontend2 |
| 68 | +│ ├── package.json |
| 69 | +│ └── webapp.config.mjs |
| 70 | +├── .env.dev # API_BASE=https://dotenv-run.dev |
| 71 | +├── .env.prod # API_BASE=https://dotenv-run.app |
| 72 | +├── .env # API_USERS=$API_BASE/api/v1/users API_AUTH=https://$API_BASE/auth |
| 73 | +├── nx.json |
| 74 | +└── package.json |
| 75 | +``` |
| 76 | + |
| 77 | +```sh |
| 78 | +$> cd /platform |
| 79 | +$> dotenv-run -e prod -- bash -c 'echo "✨ $API_USERS"' |
| 80 | +✔ /platform/.env.prod |
| 81 | +✔ /platform/.env |
| 82 | +✨ https://dotenv-run.app/api/v1/users |
| 83 | +``` |
| 84 | + |
| 85 | +```sh |
| 86 | +$> cd /platform/apps/frontend1 |
| 87 | +$> dotenv-run -e dev -- bash -c 'printf "✨ API_USERS $API_USERS\n✨ API_AUTH $API_AUTH"' |
| 88 | +✔ /platform/apps/frontend1/.env.local |
| 89 | +✔ /platform/.env.dev |
| 90 | +✔ /platform/.env |
| 91 | +✨ API_USERS http://localhost:3001/users |
| 92 | +✨ API_AUTH https://dotenv-run.dev/api/v1/auth |
| 93 | +``` |
| 94 | + |
| 95 | +```sh |
| 96 | +$> cd /platform/apps/frontend2 |
| 97 | +$> API_BASE=$CI_CONTAINER_API dotenv-run -- bash -c 'echo "✨ $API_USERS"' |
| 98 | +✔ /platform/.env |
| 99 | +✨ https://XAE221D1DE-ci-provider.cloud/api/v1/users |
| 100 | + |
| 101 | +# CI_CONTAINER_API could be an environment variable provided by some CI provider |
| 102 | +``` |
| 103 | + |
| 104 | +**-r option** |
| 105 | + |
| 106 | +```sh |
| 107 | +$> cd /platform/apps/frontend1 |
| 108 | +$> dotenv-run -r . -- bash -c 'echo "✨ $API_USERS"' |
| 109 | +✔ /platform/apps/frontend1/.env.local |
| 110 | +✨ http://localhost:3001/users |
| 111 | +``` |
| 112 | + |
| 113 | +Paths to the root workspace can be relative or absolute, the following are all valid : |
| 114 | + |
| 115 | +- ` -r ../..` |
| 116 | +- ` -r ../...env` |
| 117 | +- ` -r /platform` |
| 118 | +- ` -r /platform/.env` |
| 119 | + |
| 120 | +## Loading Priorities |
| 121 | + |
| 122 | +`@dotenv-run/cli` uses [dotenv](https://github.com/motdotla/dotenv) to support loading environment variables from `.env` files. |
| 123 | + |
| 124 | +`@dotenv-run/cli` loads `.env` files with these specific names for the following `-e ENV` value, files on the top have less priority than files on the bottom. |
| 125 | + |
| 126 | +An env file for a specific mode (e.g. .env.production) will take higher priority than a generic one (e.g. .env). |
| 127 | + |
| 128 | +| valid `.env` filenames | `ENV=*` | `ENV=test` | |
| 129 | +| ---------------------- | ------- | ---------- | |
| 130 | +| `.env` | ✔️ | ✔️ | |
| 131 | +| `.env.local` | ✔️ | ✖️ | |
| 132 | +| `.env.${ENV}` | ✔️ | ✔️ | |
| 133 | +| `.env.${ENV}.local` | ✔️ | ✔️ | |
| 134 | + |
| 135 | +In addition, environment variables that already exist when the CLI is executed have the highest priority and will not be overwritten by .env files. For example, when running `SOME_KEY=123 dotenv-run`. |
| 136 | + |
| 137 | +## Expanding `.env` |
| 138 | + |
| 139 | +You can expand variables already available on your machine for use in your `.env` |
| 140 | + |
| 141 | +For example: |
| 142 | + |
| 143 | +```shell |
| 144 | +VERSION=$npm_package_version |
| 145 | +HOSTNAME=$HOSTNAME |
| 146 | +``` |
| 147 | + |
| 148 | +Or expand variables local to the current `.env` file: |
| 149 | + |
| 150 | +```shell |
| 151 | +DOMAIN=www.example.com |
| 152 | +FOO=$DOMAIN/foo |
| 153 | +BAR=$DOMAIN/bar |
| 154 | +``` |
| 155 | + |
| 156 | +## Command Line |
| 157 | + |
| 158 | +Defining environment variables can vary between OSes. It’s also important to know that this manner is temporary for the life of the shell session. |
| 159 | + |
| 160 | +### Windows (cmd.exe) |
| 161 | + |
| 162 | +```cmd |
| 163 | +set "API_URL=abcdef" && dotenv-run -- npm start |
| 164 | +``` |
| 165 | + |
| 166 | +(Note: Quotes around the variable assignment are required to avoid a trailing whitespace.) |
| 167 | + |
| 168 | +### Windows (Powershell) |
| 169 | + |
| 170 | +```Powershell |
| 171 | +($env:API_URL = "abcdef") -and (dotenv-run -- npm start) |
| 172 | +``` |
| 173 | + |
| 174 | +### Linux, macOS (Bash) |
| 175 | + |
| 176 | +```sh |
| 177 | +API_URL=abcdef dotenv-run -- npm start |
| 178 | +``` |
| 179 | + |
| 180 | +# In the browser |
| 181 | + |
| 182 | +In order to consume environment variables in your webapps, you need to expose them to the browser. The bundler you use will need to support replacing the environment variables at build time. |
| 183 | + |
| 184 | +**React, Vue.js...** |
| 185 | + |
| 186 | +Use [Vite](https://vitejs.dev/guide/env-and-mode.html) |
| 187 | + |
| 188 | +**Angular** |
| 189 | + |
| 190 | +Use [@ngx-env/builder](https://www.npmjs.com/package/@ngx-env/builder) |
| 191 | + |
| 192 | +# License |
| 193 | + |
| 194 | +MIT © [Chihab Otmani](https://twitter.com/chihabotmani) |
0 commit comments