@mrgibson/dotenv-buffer 17.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,679 @@
1
+ <div align="center">
2
+ 🎉 announcing <a href="https://github.com/dotenvx/dotenvx">dotenvx</a>. <em>run anywhere, multi-environment, encrypted envs</em>.
3
+ </div>
4
+
5
+ &nbsp;
6
+
7
+ # dotenv [![NPM version](https://img.shields.io/npm/v/dotenv.svg?style=flat-square)](https://www.npmjs.com/package/dotenv)
8
+
9
+ <img src="https://raw.githubusercontent.com/motdotla/dotenv/master/dotenv.svg" alt="dotenv" align="right" width="200" />
10
+
11
+ 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](https://12factor.net/config) methodology.
12
+
13
+ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
14
+ [![LICENSE](https://img.shields.io/github/license/motdotla/dotenv.svg)](LICENSE)
15
+ [![codecov](https://codecov.io/gh/motdotla/dotenv-expand/graph/badge.svg?token=pawWEyaMfg)](https://codecov.io/gh/motdotla/dotenv-expand)
16
+
17
+ * [🌱 Install](#-install)
18
+ * [🏗️ Usage (.env)](#%EF%B8%8F-usage)
19
+ * [🌴 Multiple Environments 🆕](#-manage-multiple-environments)
20
+ * [🚀 Deploying (encryption) 🆕](#-deploying)
21
+ * [📚 Examples](#-examples)
22
+ * [📖 Docs](#-documentation)
23
+ * [❓ FAQ](#-faq)
24
+ * [⏱️ Changelog](./CHANGELOG.md)
25
+
26
+ ## 🌱 Install
27
+
28
+ ```bash
29
+ npm install dotenv --save
30
+ ```
31
+
32
+ You can also use an npm-compatible package manager like yarn, bun or pnpm:
33
+
34
+ ```bash
35
+ yarn add dotenv
36
+ ```
37
+ ```bash
38
+ bun add dotenv
39
+ ```
40
+ ```bash
41
+ pnpm add dotenv
42
+ ```
43
+
44
+ ## 🏗️ Usage
45
+
46
+ <a href="https://www.youtube.com/watch?v=YtkZR0NFd1g">
47
+ <div align="right">
48
+ <img src="https://img.youtube.com/vi/YtkZR0NFd1g/hqdefault.jpg" alt="how to use dotenv video tutorial" align="right" width="330" />
49
+ <img src="https://simpleicons.vercel.app/youtube/ff0000" alt="youtube/@dotenvorg" align="right" width="24" />
50
+ </div>
51
+ </a>
52
+
53
+ Create a `.env` file in the root of your project (if using a monorepo structure like `apps/backend/app.js`, put it in the root of the folder where your `app.js` process runs):
54
+
55
+ ```dosini
56
+ S3_BUCKET="YOURS3BUCKET"
57
+ SECRET_KEY="YOURSECRETKEYGOESHERE"
58
+ ```
59
+
60
+ As early as possible in your application, import and configure dotenv:
61
+
62
+ ```javascript
63
+ require('dotenv').config()
64
+ console.log(process.env) // remove this after you've confirmed it is working
65
+ ```
66
+
67
+ .. [or using ES6?](#how-do-i-use-dotenv-with-import)
68
+
69
+ ```javascript
70
+ import 'dotenv/config'
71
+ ```
72
+
73
+ ES6 import if you need to set config options:
74
+
75
+ ```javascript
76
+ import dotenv from 'dotenv'
77
+
78
+ dotenv.config({ path: '/custom/path/to/.env' })
79
+ ```
80
+
81
+ That's it. `process.env` now has the keys and values you defined in your `.env` file:
82
+
83
+ ```javascript
84
+ require('dotenv').config()
85
+ // or import 'dotenv/config' if you're using ES6
86
+
87
+ ...
88
+
89
+ s3.getBucketCors({Bucket: process.env.S3_BUCKET}, function(err, data) {})
90
+ ```
91
+
92
+ ### Multiline values
93
+
94
+ If you need multiline variables, for example private keys, those are now supported (`>= v15.0.0`) with line breaks:
95
+
96
+ ```dosini
97
+ PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
98
+ ...
99
+ Kh9NV...
100
+ ...
101
+ -----END RSA PRIVATE KEY-----"
102
+ ```
103
+
104
+ Alternatively, you can double quote strings and use the `\n` character:
105
+
106
+ ```dosini
107
+ PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nKh9NV...\n-----END RSA PRIVATE KEY-----\n"
108
+ ```
109
+
110
+ ### Comments
111
+
112
+ Comments may be added to your file on their own line or inline:
113
+
114
+ ```dosini
115
+ # This is a comment
116
+ SECRET_KEY=YOURSECRETKEYGOESHERE # comment
117
+ SECRET_HASH="something-with-a-#-hash"
118
+ ```
119
+
120
+ Comments begin where a `#` exists, so if your value contains a `#` please wrap it in quotes. This is a breaking change from `>= v15.0.0` and on.
121
+
122
+ ### Parsing
123
+
124
+ The engine which parses the contents of your file containing environment variables is available to use. It accepts a String or Buffer and will return an Object with the parsed keys and values.
125
+
126
+ ```javascript
127
+ const dotenv = require('dotenv')
128
+ const buf = Buffer.from('BASIC=basic')
129
+ const config = dotenv.parse(buf) // will return an object
130
+ console.log(typeof config, config) // object { BASIC : 'basic' }
131
+ ```
132
+
133
+ ### Preload
134
+
135
+ > Note: Consider using [`dotenvx`](https://github.com/dotenvx/dotenvx) instead of preloading. I am now doing (and recommending) so.
136
+ >
137
+ > It serves the same purpose (you do not need to require and load dotenv), adds better debugging, and works with ANY language, framework, or platform. – [motdotla](https://github.com/motdotla)
138
+
139
+ You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#-r---require-module) to preload dotenv. By doing this, you do not need to require and load dotenv in your application code.
140
+
141
+ ```bash
142
+ $ node -r dotenv/config your_script.js
143
+ ```
144
+
145
+ The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value`
146
+
147
+ ```bash
148
+ $ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env dotenv_config_debug=true
149
+ ```
150
+
151
+ Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.
152
+
153
+ ```bash
154
+ $ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js
155
+ ```
156
+
157
+ ```bash
158
+ $ DOTENV_CONFIG_ENCODING=latin1 DOTENV_CONFIG_DEBUG=true node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
159
+ ```
160
+
161
+ ### Variable Expansion
162
+
163
+ Use [dotenvx](https://github.com/dotenvx/dotenvx) to use variable expansion.
164
+
165
+ Reference and expand variables already on your machine for use in your .env file.
166
+
167
+ ```ini
168
+ # .env
169
+ USERNAME="username"
170
+ DATABASE_URL="postgres://${USERNAME}@localhost/my_database"
171
+ ```
172
+ ```js
173
+ // index.js
174
+ console.log('DATABASE_URL', process.env.DATABASE_URL)
175
+ ```
176
+ ```sh
177
+ $ dotenvx run --debug -- node index.js
178
+ [dotenvx@0.14.1] injecting env (2) from .env
179
+ DATABASE_URL postgres://username@localhost/my_database
180
+ ```
181
+
182
+ ### Command Substitution
183
+
184
+ Use [dotenvx](https://github.com/dotenvx/dotenvx) to use command substitution.
185
+
186
+ Add the output of a command to one of your variables in your .env file.
187
+
188
+ ```ini
189
+ # .env
190
+ DATABASE_URL="postgres://$(whoami)@localhost/my_database"
191
+ ```
192
+ ```js
193
+ // index.js
194
+ console.log('DATABASE_URL', process.env.DATABASE_URL)
195
+ ```
196
+ ```sh
197
+ $ dotenvx run --debug -- node index.js
198
+ [dotenvx@0.14.1] injecting env (1) from .env
199
+ DATABASE_URL postgres://yourusername@localhost/my_database
200
+ ```
201
+
202
+ ### Syncing
203
+
204
+ You need to keep `.env` files in sync between machines, environments, or team members? Use [dotenvx](https://github.com/dotenvx/dotenvx) to encrypt your `.env` files and safely include them in source control. This still subscribes to the twelve-factor app rules by generating a decryption key separate from code.
205
+
206
+ ### Multiple Environments
207
+
208
+ Use [dotenvx](https://github.com/dotenvx/dotenvx) to generate `.env.ci`, `.env.production` files, and more.
209
+
210
+ ### Deploying
211
+
212
+ You need to deploy your secrets in a cloud-agnostic manner? Use [dotenvx](https://github.com/dotenvx/dotenvx) to generate a private decryption key that is set on your production server.
213
+
214
+ ## 🌴 Manage Multiple Environments
215
+
216
+ Use [dotenvx](https://github.com/dotenvx/dotenvx)
217
+
218
+ Run any environment locally. Create a `.env.ENVIRONMENT` file and use `--env-file` to load it. It's straightforward, yet flexible.
219
+
220
+ ```bash
221
+ $ echo "HELLO=production" > .env.production
222
+ $ echo "console.log('Hello ' + process.env.HELLO)" > index.js
223
+
224
+ $ dotenvx run --env-file=.env.production -- node index.js
225
+ Hello production
226
+ > ^^
227
+ ```
228
+
229
+ or with multiple .env files
230
+
231
+ ```bash
232
+ $ echo "HELLO=local" > .env.local
233
+ $ echo "HELLO=World" > .env
234
+ $ echo "console.log('Hello ' + process.env.HELLO)" > index.js
235
+
236
+ $ dotenvx run --env-file=.env.local --env-file=.env -- node index.js
237
+ Hello local
238
+ ```
239
+
240
+ [more environment examples](https://dotenvx.com/docs/quickstart/environments)
241
+
242
+ ## 🚀 Deploying
243
+
244
+ Use [dotenvx](https://github.com/dotenvx/dotenvx).
245
+
246
+ Add encryption to your `.env` files with a single command. Pass the `--encrypt` flag.
247
+
248
+ ```
249
+ $ dotenvx set HELLO Production --encrypt -f .env.production
250
+ $ echo "console.log('Hello ' + process.env.HELLO)" > index.js
251
+
252
+ $ DOTENV_PRIVATE_KEY_PRODUCTION="<.env.production private key>" dotenvx run -- node index.js
253
+ [dotenvx] injecting env (2) from .env.production
254
+ Hello Production
255
+ ```
256
+
257
+ [learn more](https://github.com/dotenvx/dotenvx?tab=readme-ov-file#encryption)
258
+
259
+ ## 📚 Examples
260
+
261
+ See [examples](https://github.com/dotenv-org/examples) of using dotenv with various frameworks, languages, and configurations.
262
+
263
+ * [nodejs](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-nodejs)
264
+ * [nodejs (debug on)](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-nodejs-debug)
265
+ * [nodejs (override on)](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-nodejs-override)
266
+ * [nodejs (processEnv override)](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-custom-target)
267
+ * [esm](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-esm)
268
+ * [esm (preload)](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-esm-preload)
269
+ * [typescript](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-typescript)
270
+ * [typescript parse](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-typescript-parse)
271
+ * [typescript config](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-typescript-config)
272
+ * [webpack](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-webpack)
273
+ * [webpack (plugin)](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-webpack2)
274
+ * [react](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-react)
275
+ * [react (typescript)](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-react-typescript)
276
+ * [express](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-express)
277
+ * [nestjs](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-nestjs)
278
+ * [fastify](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-fastify)
279
+
280
+ ## 📖 Documentation
281
+
282
+ Dotenv exposes four functions:
283
+
284
+ * `config`
285
+ * `parse`
286
+ * `populate`
287
+
288
+ ### Config
289
+
290
+ `config` will read your `.env` file, parse the contents, assign it to
291
+ [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env),
292
+ and return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.
293
+
294
+ ```js
295
+ const result = dotenv.config()
296
+
297
+ if (result.error) {
298
+ throw result.error
299
+ }
300
+
301
+ console.log(result.parsed)
302
+ ```
303
+
304
+ You can additionally, pass options to `config`.
305
+
306
+ #### Options
307
+
308
+ ##### path
309
+
310
+ Default: `path.resolve(process.cwd(), '.env')`
311
+
312
+ Specify a custom path if your file containing environment variables is located elsewhere.
313
+
314
+ ```js
315
+ require('dotenv').config({ path: '/custom/path/to/.env' })
316
+ ```
317
+
318
+ By default, `config` will look for a file called .env in the current working directory.
319
+
320
+ Pass in multiple files as an array, and they will be parsed in order and combined with `process.env` (or `option.processEnv`, if set). The first value set for a variable will win, unless the `options.override` flag is set, in which case the last value set will win. If a value already exists in `process.env` and the `options.override` flag is NOT set, no changes will be made to that value.
321
+
322
+ ```js
323
+ require('dotenv').config({ path: ['.env.local', '.env'] })
324
+ ```
325
+
326
+ ##### quiet
327
+
328
+ Default: `false`
329
+
330
+ Suppress runtime logging message.
331
+
332
+ ```js
333
+ // index.js
334
+ require('dotenv').config({ quiet: false }) // change to true to suppress
335
+ console.log(`Hello ${process.env.HELLO}`)
336
+ ```
337
+
338
+ ```ini
339
+ # .env
340
+ .env
341
+ ```
342
+
343
+ ```sh
344
+ $ node index.js
345
+ [dotenv@17.0.0] injecting env (1) from .env
346
+ Hello World
347
+ ```
348
+
349
+ ##### encoding
350
+
351
+ Default: `utf8`
352
+
353
+ Specify the encoding of your file containing environment variables.
354
+
355
+ ```js
356
+ require('dotenv').config({ encoding: 'latin1' })
357
+ ```
358
+
359
+ ##### debug
360
+
361
+ Default: `false`
362
+
363
+ Turn on logging to help debug why certain keys or values are not being set as you expect.
364
+
365
+ ```js
366
+ require('dotenv').config({ debug: process.env.DEBUG })
367
+ ```
368
+
369
+ ##### override
370
+
371
+ Default: `false`
372
+
373
+ Override any environment variables that have already been set on your machine with values from your .env file(s). If multiple files have been provided in `option.path` the override will also be used as each file is combined with the next. Without `override` being set, the first value wins. With `override` set the last value wins.
374
+
375
+ ```js
376
+ require('dotenv').config({ override: true })
377
+ ```
378
+
379
+ ##### processEnv
380
+
381
+ Default: `process.env`
382
+
383
+ Specify an object to write your environment variables to. Defaults to `process.env` environment variables.
384
+
385
+ ```js
386
+ const myObject = {}
387
+ require('dotenv').config({ processEnv: myObject })
388
+
389
+ console.log(myObject) // values from .env
390
+ console.log(process.env) // this was not changed or written to
391
+ ```
392
+
393
+ ### Parse
394
+
395
+ The engine which parses the contents of your file containing environment
396
+ variables is available to use. It accepts a String or Buffer and will return
397
+ an Object with the parsed keys and values.
398
+
399
+ ```js
400
+ const dotenv = require('dotenv')
401
+ const buf = Buffer.from('BASIC=basic')
402
+ const config = dotenv.parse(buf) // will return an object
403
+ console.log(typeof config, config) // object { BASIC : 'basic' }
404
+ ```
405
+
406
+ #### Options
407
+
408
+ ##### debug
409
+
410
+ Default: `false`
411
+
412
+ Turn on logging to help debug why certain keys or values are not being set as you expect.
413
+
414
+ ```js
415
+ const dotenv = require('dotenv')
416
+ const buf = Buffer.from('hello world')
417
+ const opt = { debug: true }
418
+ const config = dotenv.parse(buf, opt)
419
+ // expect a debug message because the buffer is not in KEY=VAL form
420
+ ```
421
+
422
+ ### Populate
423
+
424
+ The engine which populates the contents of your .env file to `process.env` is available for use. It accepts a target, a source, and options. This is useful for power users who want to supply their own objects.
425
+
426
+ For example, customizing the source:
427
+
428
+ ```js
429
+ const dotenv = require('dotenv')
430
+ const parsed = { HELLO: 'world' }
431
+
432
+ dotenv.populate(process.env, parsed)
433
+
434
+ console.log(process.env.HELLO) // world
435
+ ```
436
+
437
+ For example, customizing the source AND target:
438
+
439
+ ```js
440
+ const dotenv = require('dotenv')
441
+ const parsed = { HELLO: 'universe' }
442
+ const target = { HELLO: 'world' } // empty object
443
+
444
+ dotenv.populate(target, parsed, { override: true, debug: true })
445
+
446
+ console.log(target) // { HELLO: 'universe' }
447
+ ```
448
+
449
+ #### options
450
+
451
+ ##### Debug
452
+
453
+ Default: `false`
454
+
455
+ Turn on logging to help debug why certain keys or values are not being populated as you expect.
456
+
457
+ ##### override
458
+
459
+ Default: `false`
460
+
461
+ Override any environment variables that have already been set.
462
+
463
+ ## ❓ FAQ
464
+
465
+ ### Why is the `.env` file not loading my environment variables successfully?
466
+
467
+ Most likely your `.env` file is not in the correct place. [See this stack overflow](https://stackoverflow.com/questions/42335016/dotenv-file-is-not-loading-environment-variables).
468
+
469
+ Turn on debug mode and try again..
470
+
471
+ ```js
472
+ require('dotenv').config({ debug: true })
473
+ ```
474
+
475
+ You will receive a helpful error outputted to your console.
476
+
477
+ ### Should I commit my `.env` file?
478
+
479
+ No. We **strongly** recommend against committing your `.env` file to version
480
+ control. It should only include environment-specific values such as database
481
+ passwords or API keys. Your production database should have a different
482
+ password than your development database.
483
+
484
+ ### Should I have multiple `.env` files?
485
+
486
+ We recommend creating one `.env` file per environment. Use `.env` for local/development, `.env.production` for production and so on. This still follows the twelve factor principles as each is attributed individually to its own environment. Avoid custom set ups that work in inheritance somehow (`.env.production` inherits values form `.env` for example). It is better to duplicate values if necessary across each `.env.environment` file.
487
+
488
+ > 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.
489
+ >
490
+ > – [The Twelve-Factor App](http://12factor.net/config)
491
+
492
+ ### What rules does the parsing engine follow?
493
+
494
+ The parsing engine currently supports the following rules:
495
+
496
+ - `BASIC=basic` becomes `{BASIC: 'basic'}`
497
+ - empty lines are skipped
498
+ - lines beginning with `#` are treated as comments
499
+ - `#` marks the beginning of a comment (unless when the value is wrapped in quotes)
500
+ - empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`)
501
+ - inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
502
+ - 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'}`)
503
+ - single and double quoted values are escaped (`SINGLE_QUOTE='quoted'` becomes `{SINGLE_QUOTE: "quoted"}`)
504
+ - single and double quoted values maintain whitespace from both ends (`FOO=" some value "` becomes `{FOO: ' some value '}`)
505
+ - double quoted values expand new lines (`MULTILINE="new\nline"` becomes
506
+
507
+ ```
508
+ {MULTILINE: 'new
509
+ line'}
510
+ ```
511
+
512
+ - backticks are supported (`` BACKTICK_KEY=`This has 'single' and "double" quotes inside of it.` ``)
513
+
514
+ ### What happens to environment variables that were already set?
515
+
516
+ By default, we 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.
517
+
518
+ If instead, you want to override `process.env` use the `override` option.
519
+
520
+ ```javascript
521
+ require('dotenv').config({ override: true })
522
+ ```
523
+
524
+ ### How come my environment variables are not showing up for React?
525
+
526
+ Your React code is run in Webpack, where the `fs` module or even the `process` global itself are not accessible out-of-the-box. `process.env` can only be injected through Webpack configuration.
527
+
528
+ If you are using [`react-scripts`](https://www.npmjs.com/package/react-scripts), which is distributed through [`create-react-app`](https://create-react-app.dev/), it has dotenv built in but with a quirk. Preface your environment variables with `REACT_APP_`. See [this stack overflow](https://stackoverflow.com/questions/42182577/is-it-possible-to-use-dotenv-in-a-react-project) for more details.
529
+
530
+ If you are using other frameworks (e.g. Next.js, Gatsby...), you need to consult their documentation for how to inject environment variables into the client.
531
+
532
+ ### Can I customize/write plugins for dotenv?
533
+
534
+ Yes! `dotenv.config()` returns an object representing the parsed `.env` file. This gives you everything you need to continue setting values on `process.env`. For example:
535
+
536
+ ```js
537
+ const dotenv = require('dotenv')
538
+ const variableExpansion = require('dotenv-expand')
539
+ const myEnv = dotenv.config()
540
+ variableExpansion(myEnv)
541
+ ```
542
+
543
+ ### How do I use dotenv with `import`?
544
+
545
+ Simply..
546
+
547
+ ```javascript
548
+ // index.mjs (ESM)
549
+ import 'dotenv/config' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
550
+ import express from 'express'
551
+ ```
552
+
553
+ A little background..
554
+
555
+ > 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.
556
+ >
557
+ > – [ES6 In Depth: Modules](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/)
558
+
559
+ What does this mean in plain language? It means you would think the following would work but it won't.
560
+
561
+ `errorReporter.mjs`:
562
+ ```js
563
+ class Client {
564
+ constructor (apiKey) {
565
+ console.log('apiKey', apiKey)
566
+
567
+ this.apiKey = apiKey
568
+ }
569
+ }
570
+
571
+ export default new Client(process.env.API_KEY)
572
+ ```
573
+ `index.mjs`:
574
+ ```js
575
+ // Note: this is INCORRECT and will not work
576
+ import * as dotenv from 'dotenv'
577
+ dotenv.config()
578
+
579
+ import errorReporter from './errorReporter.mjs' // process.env.API_KEY will be blank!
580
+ ```
581
+
582
+ `process.env.API_KEY` will be blank.
583
+
584
+ Instead, `index.mjs` should be written as..
585
+
586
+ ```js
587
+ import 'dotenv/config'
588
+
589
+ import errorReporter from './errorReporter.mjs'
590
+ ```
591
+
592
+ Does that make sense? It's a bit unintuitive, but it is how importing of ES6 modules work. Here is a [working example of this pitfall](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-es6-import-pitfall).
593
+
594
+ There are two alternatives to this approach:
595
+
596
+ 1. Preload with dotenvx: `dotenvx run -- node index.js` (_Note: you do not need to `import` dotenv with this approach_)
597
+ 2. 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)
598
+
599
+ ### Why am I getting the error `Module not found: Error: Can't resolve 'crypto|os|path'`?
600
+
601
+ You are using dotenv on the front-end and have not included a polyfill. Webpack < 5 used to include these for you. Do the following:
602
+
603
+ ```bash
604
+ npm install node-polyfill-webpack-plugin
605
+ ```
606
+
607
+ Configure your `webpack.config.js` to something like the following.
608
+
609
+ ```js
610
+ require('dotenv').config()
611
+
612
+ const path = require('path');
613
+ const webpack = require('webpack')
614
+
615
+ const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
616
+
617
+ module.exports = {
618
+ mode: 'development',
619
+ entry: './src/index.ts',
620
+ output: {
621
+ filename: 'bundle.js',
622
+ path: path.resolve(__dirname, 'dist'),
623
+ },
624
+ plugins: [
625
+ new NodePolyfillPlugin(),
626
+ new webpack.DefinePlugin({
627
+ 'process.env': {
628
+ HELLO: JSON.stringify(process.env.HELLO)
629
+ }
630
+ }),
631
+ ]
632
+ };
633
+ ```
634
+
635
+ Alternatively, just use [dotenv-webpack](https://github.com/mrsteele/dotenv-webpack) which does this and more behind the scenes for you.
636
+
637
+ ### What about variable expansion?
638
+
639
+ Try [dotenv-expand](https://github.com/motdotla/dotenv-expand)
640
+
641
+ ### What about syncing and securing .env files?
642
+
643
+ Use [dotenvx](https://github.com/dotenvx/dotenvx) to unlock syncing encrypted .env files over git.
644
+
645
+ ### What if I accidentally commit my `.env` file to code?
646
+
647
+ Remove it, [remove git history](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository) and then install the [git pre-commit hook](https://github.com/dotenvx/dotenvx#pre-commit) to prevent this from ever happening again.
648
+
649
+ ```
650
+ brew install dotenvx/brew/dotenvx
651
+ dotenvx precommit --install
652
+ ```
653
+
654
+ ### How can I prevent committing my `.env` file to a Docker build?
655
+
656
+ Use the [docker prebuild hook](https://dotenvx.com/docs/features/prebuild).
657
+
658
+ ```bash
659
+ # Dockerfile
660
+ ...
661
+ RUN curl -fsS https://dotenvx.sh/ | sh
662
+ ...
663
+ RUN dotenvx prebuild
664
+ CMD ["dotenvx", "run", "--", "node", "index.js"]
665
+ ```
666
+
667
+ ## Contributing Guide
668
+
669
+ See [CONTRIBUTING.md](CONTRIBUTING.md)
670
+
671
+ ## CHANGELOG
672
+
673
+ See [CHANGELOG.md](CHANGELOG.md)
674
+
675
+ ## Who's using dotenv?
676
+
677
+ [These npm modules depend on it.](https://www.npmjs.com/browse/depended/dotenv)
678
+
679
+ Projects that expand it often use the [keyword "dotenv" on npm](https://www.npmjs.com/search?q=keywords:dotenv).
package/SECURITY.md ADDED
@@ -0,0 +1 @@
1
+ Please report any security vulnerabilities to security@dotenvx.com.
package/config.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/config.js ADDED
@@ -0,0 +1,9 @@
1
+ (function () {
2
+ require('./lib/main').config(
3
+ Object.assign(
4
+ {},
5
+ require('./lib/env-options'),
6
+ require('./lib/cli-options')(process.argv)
7
+ )
8
+ )
9
+ })()