Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When in debug mode log error of loading .env file #594

Merged
merged 3 commits into from Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions CHANGELOG.md
Expand Up @@ -4,11 +4,17 @@ All notable changes to this project will be documented in this file. See [standa

## [Unreleased](https://github.com/motdotla/dotenv/compare/v14.0.0...master)

## [14.0.1](https://github.com/motdotla/dotenv/compare/v14.0.0...v14.0.1) (2022-01-16)

### Added

- Log error on failure to load `.env` file ([#594](https://github.com/motdotla/dotenv/pull/594))

## [14.0.0](https://github.com/motdotla/dotenv/compare/v13.0.1...v14.0.0) (2022-01-16)

### Added

- Support inline comments for the parser 🎉 ([#568](https://github.com/motdotla/dotenv/pull/568))
- _Breaking:_ Support inline comments for the parser 🎉 ([#568](https://github.com/motdotla/dotenv/pull/568))

## [13.0.1](https://github.com/motdotla/dotenv/compare/v13.0.0...v13.0.1) (2022-01-16)

Expand All @@ -20,7 +26,7 @@ All notable changes to this project will be documented in this file. See [standa

### Added

* Add type file for `config.js` ([#539](https://github.com/motdotla/dotenv/pull/539))
* _Breaking:_ Add type file for `config.js` ([#539](https://github.com/motdotla/dotenv/pull/539))

## [12.0.4](https://github.com/motdotla/dotenv/compare/v12.0.3...v12.0.4) (2022-01-16)

Expand Down
18 changes: 14 additions & 4 deletions README.md
Expand Up @@ -46,8 +46,7 @@ DB_PASS=s1mpl3

```javascript
// index.js
const dotenv = require('dotenv')
dotenv.config()
require('dotenv').config()

console.log(process.env) // remove this after you've confirmed it working
```
Expand All @@ -65,8 +64,7 @@ import express from 'express'
`process.env` now has the keys and values you defined in your `.env` file.

```javascript
var dotenv = require('dotenv')
dotenv.config()
require('dotenv').config()

...

Expand Down Expand Up @@ -209,6 +207,18 @@ $ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv/config your_script.js dotenv_conf

## FAQ

### Why is the `.env` file not loading my environment variables successfully?

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

Turn on debug mode and try again..

```js
require('dotenv').config({ debug: true })
```

You will receive a helpful error outputted to your console.

### Should I commit my `.env` file?

No. We **strongly** recommend against committing your `.env` file to version
Expand Down
7 changes: 5 additions & 2 deletions lib/main.js
Expand Up @@ -48,7 +48,7 @@ function parse (src, options) {

// ignore empty and commented lines
if (trimmedLine.length && trimmedLine[0] !== '#') {
log(`failed to match key and value when parsing line ${idx + 1}: ${line}`)
log(`Failed to match key and value when parsing line ${idx + 1}: ${line}`)
}
}
})
Expand Down Expand Up @@ -89,7 +89,10 @@ function config (options) {

return { parsed }
} catch (e) {
console.error(e);
if (debug) {
log(`Failed to load ${dotenvPath} ${e.message}`)
}

return { error: e }
}
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "dotenv",
"version": "14.0.0",
"version": "14.0.1",
"description": "Loads environment variables from .env file",
"main": "lib/main.js",
"types": "lib/main.d.ts",
Expand Down
16 changes: 15 additions & 1 deletion tests/test-config.js
Expand Up @@ -11,7 +11,7 @@ const mockParseResponse = { test: 'foo' }
let readFileSyncStub
let parseStub

t.plan(9)
t.plan(10)

t.beforeEach(done => {
readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
Expand Down Expand Up @@ -119,3 +119,17 @@ t.test('returns any errors thrown from reading file or parsing', ct => {

ct.type(env.error, Error)
})

t.test('logs any errors thrown from reading file or parsing when in debug mode', ct => {
ct.plan(2)

const logStub = sinon.stub(console, 'log')

readFileSyncStub.throws()
const env = dotenv.config({ debug: true })

ct.ok(logStub.called)
ct.type(env.error, Error)

logStub.restore()
})