Skip to content

Commit

Permalink
update example to be duplicatable with simple copy/paste
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Feb 19, 2025
1 parent d65539a commit 6da8ddc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,13 @@ What does this mean in plain language? It means you would think the following wo

`errorReporter.mjs`:
```js
import { Client } from 'best-error-reporting-service'
class Client {
constructor (apiKey) {
console.log('apiKey', apiKey)

this.apiKey = apiKey
}
}

export default new Client(process.env.API_KEY)
```
Expand All @@ -546,8 +552,7 @@ export default new Client(process.env.API_KEY)
import * as dotenv from 'dotenv'
dotenv.config()

import errorReporter from './errorReporter.mjs'
errorReporter.report(new Error('documented example'))
import errorReporter from './errorReporter.mjs' // process.env.API_KEY will be blank!
```

`process.env.API_KEY` will be blank.
Expand All @@ -558,7 +563,6 @@ Instead, `index.mjs` should be written as..
import 'dotenv/config'

import errorReporter from './errorReporter.mjs'
errorReporter.report(new Error('documented example'))
```

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).
Expand Down

0 comments on commit 6da8ddc

Please sign in to comment.