@@ -138,6 +138,95 @@ console.log(process.env.EXPONENTIAL) // (string) 'false'
138
138
console .log (process .env .NUMBER ) // (string) '100'
139
139
```
140
140
141
+ ## Preload
142
+
143
+ You can use the ` --require ` (` -r ` ) [ command line option] ( https://nodejs.org/api/cli.html#cli_r_require_module )
144
+ to preload dotenv & dotenv-conversion (and even dotenv-expand).
145
+ By doing this, you do not need to require and load dotenv or dotenv-conversion (or dotenv-expand)
146
+ in your application code.
147
+ This is the preferred approach when using ` import ` instead of ` require ` .
148
+
149
+ ``` bash
150
+ # dotenv + dotenv-conversion
151
+ $ node -r dotenv-conversion/config your_script.js
152
+
153
+ # dotenv + dotenv-expand + dotenv-conversion
154
+ $ node -r dotenv-conversion/config-expand your_script.js
155
+ ```
156
+
157
+ The configuration options below are supported as command line arguments
158
+ in the format ` dotenv_config_<option>=value ` .
159
+
160
+ ``` bash
161
+ # dotenv + dotenv-conversion
162
+ $ node -r dotenv-conversion/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars
163
+
164
+ # dotenv + dotenv-expand + dotenv-conversion
165
+ $ node -r dotenv-conversion/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars
166
+ ```
167
+
168
+ Additionally, you can use environment variables to set configuration options.
169
+ Command line arguments will precede these.
170
+
171
+ ``` bash
172
+ # dotenv + dotenv-conversion
173
+ $ DOTENV_CONFIG_< OPTION> =value node -r dotenv-conversion/config your_script.js
174
+
175
+ # dotenv + dotenv-expand + dotenv-conversion
176
+ $ DOTENV_CONFIG_< OPTION> =value node -r dotenv-conversion/config your_script.js
177
+ ```
178
+
179
+ ``` bash
180
+ # dotenv + dotenv-conversion
181
+ $ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv-conversion/config your_script.js dotenv_config_path=/custom/path/to/.env
182
+
183
+ # dotenv + dotenv-expand + dotenv-conversion
184
+ $ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv-conversion/config your_script.js dotenv_config_path=/custom/path/to/.env
185
+ ```
186
+
187
+ After preload, you can retrieve converted variables via ` global.dotenvConversion.parsed ` :
188
+
189
+ ``` dotenv
190
+ # .env file
191
+ DEBUG_LEVEL=0
192
+ DEBUG=boolean:$DEBUG_LEVEL
193
+
194
+ EXPONENTIAL=2
195
+ NUMBER=1e$EXPONENTIAL
196
+ ```
197
+
198
+ ``` javascript
199
+ // index.js file
200
+ const parsedEnv = global .dotenvConversion .parsed
201
+ console .log (parsedEnv .DEBUG_LEVEL ) // (number) 0
202
+ console .log (parsedEnv .DEBUG ) // (boolean) false
203
+ console .log (parsedEnv .EXPONENTIAL ) // (boolean) 2
204
+ console .log (parsedEnv .NUMBER ) // (boolean) 100
205
+ console .log (process .env .DEBUG_LEVEL ) // (string) '0'
206
+ console .log (process .env .DEBUG ) // (string) 'false'
207
+ console .log (process .env .EXPONENTIAL ) // (string) 'false'
208
+ console .log (process .env .NUMBER ) // (string) '100'
209
+ ```
210
+
211
+ ``` bash
212
+ # dotenv + dotenv-expand + dotenv-conversion
213
+ $ node -r dotenv-conversion/config-expand index.js
214
+ ```
215
+
216
+ Console output:
217
+
218
+ ```
219
+ 0
220
+ false
221
+ 2
222
+ 100
223
+ 0
224
+ false
225
+ 2
226
+ 100
227
+ ```
228
+
229
+
141
230
## Features
142
231
143
232
### Auto-Conversion
0 commit comments