Configuration guides

Trim a Dotenv File Without Losing Quoted Lines

·

Before reducing a .env file, find out how your loader treats a hash in unquoted text. The same characters can begin a comment in one loader and belong to the value in another.

ENV Compressor

Compare two comment rules on the release sample

Choose Load release sample and Compress .env. With the whitespace rule, the result drops from 221 to 91 UTF-8 bytes. The unquoted value build#gray stays as written because the hash touches the preceding word.

Change Unquoted hash rule to Node.js and compress again. The output is 86 bytes. The PRIVATE_TAG value becomes build because an unquoted hash starts a comment under Node.js rules. The quoted banner keeps its hash in both runs.

Compare two comment rules on the release sample
ValueWhitespace ruleNode.js rule
API_PORT0044300443
BANNERDeploy #2Deploy #2
PRIVATE_TAGbuild#graybuild
EMPTYEmpty stringEmpty string

Open ENV Compressor and load the release sampleNode.js dotenv values, comments and parserDocker Compose env file syntax

Check the blank line inside NOTE

Open the downloaded compressed.env. The NOTE assignment still spans three physical lines. The blank middle line and the two spaces before second are part of its value. The blank line between assignments is gone.

For the Node.js result, parse the original and the download with util.parseEnv and compare their returned string maps. Use the actual destination loader for the other comment rule because loaders can also differ on quote and interpolation behavior.

Open ENV Compressor and load the release sampleNode.js dotenv values, comments and parserDocker Compose env file syntax

Resolve ambiguous empty assignments first

An empty assignment with only spaces after the equals sign can behave differently in some Node.js releases. If compression pauses on that line, write the intended empty string as KEY="" and check the source again. Use text without a leading byte-order mark in Node.js mode.

The download still carries deployment values. Use the preview locally and share a separate template if the receiving person needs only the keys.

Node.js dotenv values, comments and parser

Common questions

Does compression remove blank lines inside quoted values?

Blank lines between assignments are removed. Blank lines inside a quoted multiline value are preserved.

Can I use the whitespace option for a Docker Compose file?

Compose documents the whitespace-before-hash rule for unquoted values. Check its other dotenv behavior and validate the result in the Compose version you deploy.

Sources and example

Put it into practice.

Related guides