Skip to content

parseEnv result properties order does not match order in input string #62736

Closed as not planned

Description

@aklinker1

Version

v24.14.1

Platform

MacOS 26.3 (25D125) ARM

Subsystem

No response

What steps will reproduce the bug?

$ node -e "
import { parseEnv } from 'node:util'
console.log(parseEnv('B=b\nA=a'))
"
{ A: 'a', B: 'b' }

How often does it reproduce? Is there a required condition?

100% of the time

What is the expected behavior? Why is that the expected behavior?

Lot's of systems support "expanding" variables, but the expansion happens in the order of the env variables in the file. So If my .env file looks like this:

PASSWORD="s1mpl3"
DB_PASS=$PASSWORD

Node ignores the order and it's impossible to accurately expand the results accurately using just the output of parseEnv - you would have to parse the original input string yourself, compute the order of the keys, then re-order the resulting object to match the correct order.

For example, with dotenv-expand:

import { expand } from 'dotenv-expand';
import { parseEnv } from 'node:util';

const parsed = parseEnv(`
PASSWORD="s1mpl3"
DB_PASS=$PASSWORD
`);
console.log(parsed); // { DB_PASS: '$PASSWORD', PASSWORD: 's1mpl3' }
expand({ parsed });
console.log(parsed); // { DB_PASS: '', PASSWORD: 's1mpl3' }

You can see that because the object properties are returned in a different order that defined, dotenv-expand cannot expand the DB_PASS correctly. Flipping the order allows for correct expansion:

import { expand } from 'dotenv-expand';

const parsed = {
  PASSWORD: 's1mpl3',
  DB_PASS: '$PASSWORD',
};
expand({ parsed });
console.log(parsed); // { PASSWORD: 's1mpl3', DB_PASS: 's1mpl3' }

What do you see instead?

Resulting object seems to be sorted alphabetically.

Additional information

Activity

  1. bakkot commented on Apr 15, 2026

    @bakkot
    Contributor

    It would be relatively easy to make this almost work (store a std::vector of names alongside the existing std::map, and iterate the vector instead of the map when initializing the JS object).

    But: JS objects always have nonnegative integer keys first, and ordered. So node could be changed so you'd get { B: 'b', A: 'a' } in your sample, but you will never be able to get e.g. { B: 'b', 1: '1' }.

  2. aklinker1 commented on Apr 15, 2026

    @aklinker1
    Author

    Alright, that all makes sense to me.

    The real problem I'm facing is I can't reliably expand env vars parsed with parseEnv with any existing library - does it make sense to the add expansion support to node?

    In the native code, there was a list or vector in the parseEnv function, so would be possible to expand them properly with the correct order at that point?

  3. github-actions commented on Jul 20, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  4. added
    stale
    on Jul 20, 2026
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

    stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions