Skip to content

Dotenv running on debug mode for any non-undefined debug option #548

@n8eloy

Description

@n8eloy

Hey all! We have noticed that passing any defined values in the debug option in config function will turn on debug mode.

e.g. all those will activate debug logs:

dotenv.config({ debug: true })
dotenv.config({ debug: false })
dotenv.config({ debug: 'foo' })
dotenv.config({ debug: 0 })
dotenv.config({ debug: {} })

The documentation mentions that debug defaults as false, but I didn't expect that passing false explicitly wouldn't make it work the same way.

What I'm exactly trying to accomplish is programmatically activate debug mode for a certain environment variable:

dotenv.config({ debug: process.env.NODE_ENV === 'development' })

The only way I've made it work was by passing an undefined or null value:

dotenv.config({ debug: process.env.NODE_ENV === 'development' ? true : undefined })

I do think this happens because options.debug is checked against null, but not against falsy values. Is this expected behavior?

dotenv/lib/main.js

Lines 83 to 97 in 27dfd3f

let dotenvPath = path.resolve(process.cwd(), '.env')
let encoding /*: string */ = 'utf8'
let debug = false
if (options) {
if (options.path != null) {
dotenvPath = resolveHome(options.path)
}
if (options.encoding != null) {
encoding = options.encoding
}
if (options.debug != null) {
debug = true
}
}

I'm using Dotenv version 8.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions