Skip to content

node env file parsing supports keys compared to dotenv #58807

Description

@dario-piotrowicz

After some testing I noticed that node.js (--env-file and related) parses keys in env files differently than dotenv

Specifically dotenv doesn't support keys with spaces or special characters while node does

You can see what I tried in the screenshot below:
different keys being run with both node,js and dotenv parsing

So I would love to understand if the current node.js behavior is the desired one, or if node should align with dotenv.

PS: dot env doesn't seem to document in details what keys syntax it supports, also in the dotenv repo's tests I could not really find any test around keys 😕

Activity

  1. dario-piotrowicz commented on Jun 23, 2025

    @dario-piotrowicz
    MemberAuthor

    cc. @anonrig , @vitaly-t 🙂

  2. dario-piotrowicz commented on Jun 23, 2025

    @dario-piotrowicz
    MemberAuthor

    PS: I'm mostly interested to see if we can get an agreement on what node should support so that we can:

    • potentially change the parsing code to ignore certain keys
    • add tests to make sure that the various keys that should be supported indeed are and those not supported are not
    • ideally also document the chosen syntax
  3. vitaly-t commented on Jun 24, 2025

    @vitaly-t
    Contributor

    INI has a far more general purpose than the one for which it is used within NodeJS - just the environment variables. To that end, we should only read that which can be considered a valid environment variable name / value, and ignore the rest.

    And from the example given, we definitely should exclude variable names that have: ", ', ?, !, >, %, ~, and no spaces!

    We should allow: letters, digits, $, -, _ and dots. However, variable name should not start with a digit (similar to JS variable names)

    In all, a valid variable should follow that of a JavaScript variable name, but also allow hyphens and dots in the name.

    Here's example of regex patterns I used some time ago to implement valid variable + section...

    • For variable name: ^\s*([\w$][\w.$]*)\s*=\s*(.*)
    • For section name: \[\s*([\w$.-]+)\s*("(.*)")?\s*]

    Here, I have marked the cases we want excluded, from your examples:
    Image

    In addition, unlike dotenv, NodeJS should NOT handle any single or double quotes for the variable values, and simply consume the whole, that's it, no processing on that!

  4. added
    dotenvIssues and PRs related to .env file parsing.
    on Jun 24, 2025
  5. tniessen commented on Jun 24, 2025

    @tniessen
    Member

    So I would love to understand if the current node.js behavior is the desired one, or if node should align with dotenv.

    The only reference for the format accepted by Node.js has been "whatever dotenv does" (see, for example, #48890 (comment)). INI files are not supported since sections are not supported and string quoting might work differently. So unless the project decides to deviate from this now, I would assume that the behavior of dotenv is the desired one.

  6. anonrig commented on Jun 24, 2025

    @anonrig
    Member

    I would assume that the behavior of dotenv is the desired one.

    I agree with @tniessen. We can take dotenv parser as the source of truth for such differences while we have env-file in experimental state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dotenvIssues and PRs related to .env file parsing.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions