Skip to content

Commit c3da64b

Browse files
authored
Merge pull request #1009 from motdotla/skill
simplify skills - less detail and more to the point
2 parents 8197a16 + 6f743b1 commit c3da64b

3 files changed

Lines changed: 236 additions & 814 deletions

File tree

README.md

Lines changed: 62 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,23 @@ Create a `.env` file in the root of your project:
2222

2323
```ini
2424
# .env
25-
S3_BUCKET="YOURS3BUCKET"
26-
SECRET_KEY="YOURSECRETKEYGOESHERE"
25+
HELLO="Dotenv"
26+
OPENAI_API_KEY="your-api-key-goes-here"
2727
```
2828

29-
And as early as possible in your application, import and configure dotenv:
29+
As early as possible in your application, import and configure dotenv:
3030

3131
```javascript
3232
// index.js
33-
require('dotenv').config() // or import 'dotenv/config' if you're using ES6
34-
...
35-
console.log(process.env) // remove this after you've confirmed it is working
33+
require('dotenv').config()
34+
// or import 'dotenv/config' // for esm
35+
36+
console.log(`Hello ${process.env.HELLO}`)
3637
```
3738
```sh
3839
$ node index.js
39-
◇ injected env (14) from .env
40+
◇ injected env (2) from .env
41+
Hello Dotenv
4042
```
4143

4244
That's it. `process.env` now has the keys and values you defined in your `.env` file.
@@ -75,8 +77,6 @@ import dotenv from 'dotenv'
7577
dotenv.config({ path: '/custom/path/to/.env' })
7678
```
7779

78-
Need to pass options like `quiet: true`? See the [FAQ below](#how-do-i-specify-config-options-with-es6-import) for common patterns. 😊
79-
8080
</details>
8181
<details><summary>bun</summary><br>
8282

@@ -423,70 +423,6 @@ There are two alternatives to this approach:
423423
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)
424424
</details>
425425

426-
<details><summary>How do I specify config options with ES6 import?</summary><br/>
427-
428-
This trips up a lot of folks (myself included the first time 😅). When using `import 'dotenv/config'`, you can't pass options directly. Here are a few practical ways to handle it:
429-
430-
**Option 1: Import and call `config()` yourself (Recommended)**
431-
432-
```javascript
433-
// index.mjs
434-
import dotenv from 'dotenv'
435-
436-
dotenv.config({
437-
quiet: true,
438-
path: '/custom/path/to/.env',
439-
debug: true
440-
})
441-
442-
// Now import everything else
443-
import express from 'express'
444-
```
445-
446-
⚠️ **Heads up:** Because ES6 imports are hoisted, put the `dotenv` import and `config()` call at the very top, before any other imports that rely on `process.env`.
447-
448-
**Option 2: Use environment variables**
449-
450-
```bash
451-
DOTENV_CONFIG_QUIET=true DOTENV_CONFIG_PATH=/custom/path/to/.env node index.mjs
452-
```
453-
454-
Then in your code you can keep the shorthand:
455-
456-
```javascript
457-
import 'dotenv/config'
458-
```
459-
460-
**Option 3: A tiny wrapper file**
461-
462-
Create `load-env.mjs`:
463-
464-
```javascript
465-
import dotenv from 'dotenv'
466-
dotenv.config({ quiet: true })
467-
```
468-
469-
Then in your main file:
470-
471-
```javascript
472-
import './load-env.mjs'
473-
import express from 'express'
474-
```
475-
476-
Not the most elegant, but it works reliably when hoisting gets in the way.
477-
478-
**Quick reference for config options:**
479-
480-
| Option | Type | Description |
481-
|--------|------|-------------|
482-
| `path` | string | Path to your `.env` file |
483-
| `encoding` | string | File encoding (default: `utf8`) |
484-
| `debug` | boolean | Turn on debug logging |
485-
| `override` | boolean | Override existing env vars |
486-
| `quiet` | boolean | Suppress all console output |
487-
488-
</details>
489-
490426
<details><summary>Can I customize/write plugins for dotenv?</summary><br/>
491427

492428
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:
@@ -536,6 +472,59 @@ npm i -g @dotenvx/dotenvx
536472
dotenvx precommit --install
537473
```
538474

475+
</details>
476+
477+
<details><summary>How do I specify config options with ES6 import?</summary><br/>
478+
479+
When using `import 'dotenv/config'`, you can't pass options directly. Here are a few ways to handle it.
480+
481+
**Option 1: Import and call `config()` yourself (Recommended)**
482+
483+
```javascript
484+
// index.mjs
485+
import dotenv from 'dotenv'
486+
487+
dotenv.config({
488+
path: '/custom/path/to/.env',
489+
debug: true
490+
})
491+
492+
// Now import everything else
493+
import express from 'express'
494+
```
495+
496+
Because ES6 imports are hoisted, put the `dotenv` import and `config()` call at the very top, before any other imports that rely on `process.env`.
497+
498+
**Option 2: Use environment variables**
499+
500+
```bash
501+
DOTENV_CONFIG_DEBUG=true DOTENV_CONFIG_PATH=/custom/path/to/.env node index.mjs
502+
```
503+
504+
Then in your code you can keep the shorthand:
505+
506+
```javascript
507+
import 'dotenv/config'
508+
```
509+
510+
**Option 3: A tiny wrapper file**
511+
512+
Create `load-env.mjs`:
513+
514+
```javascript
515+
import dotenv from 'dotenv'
516+
dotenv.config({ path: '/custom/path/to/.env', debug: true })
517+
```
518+
519+
Then in your main file:
520+
521+
```javascript
522+
import './load-env.mjs'
523+
import express from 'express'
524+
```
525+
526+
Not the most elegant, but it works reliably when hoisting gets in the way.
527+
539528
</details>
540529
<details><summary>What happens to environment variables that were already set?</summary><br/>
541530

0 commit comments

Comments
 (0)