Version
v24.14.1
Platform
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
Version
v24.14.1
Platform
Subsystem
No response
What steps will reproduce the bug?
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
.envfile looks like this: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:You can see that because the object properties are returned in a different order that defined,
dotenv-expandcannot expand theDB_PASScorrectly. Flipping the order allows for correct expansion:What do you see instead?
Resulting object seems to be sorted alphabetically.
Additional information