How to add comments to .env file?

node.jsDotenv

node.js Problem Overview


I am using dotenv module to load environment variables from .env file.

.env:

# config
DAILY_REPORT_SCHEDULE='*/1 * * * *'
PORT=8080
NODE_ENV=development
DOTENV_DEBUG=true

# credentials
PROJECT_ID=shadowsocks-218808
KEY_FILE_NAME='/Users/ldu020/workspace/nodejs-gcp/.gcp/shadowsocks-218808-7f8e109f4089.json'

As you can see, I add two comments within .env file.

dotenv.js:

require('dotenv').config({ debug: process.env.DOTENV_DEBUG === 'true' });

dotenv give me debug messages:

[dotenv][DEBUG] did not match key and value when parsing line 1: # config
[dotenv][DEBUG] did not match key and value when parsing line 6:
[dotenv][DEBUG] did not match key and value when parsing line 7: # credentials
[dotenv][DEBUG] did not match key and value when parsing line 10:
[dotenv][DEBUG] did not match key and value when parsing line 11:

I know the reason why got these debug messages is I added two comments and some new line within .env file. dotenv does not parse .env file correctly.

How can I solve this?

node.js Solutions


Solution 1 - node.js

It is possible as of mid-2019.

Start the line with # symbol. See the docs:

> lines beginning with # are treated as comments.

For vlucas/phpdotenv the same situation.

The inline comments are not supported for sure for motdotla/dotenv.

Solution 2 - node.js

As of 2022-04-17, both comment lines and inline comments are available. Just use #.

Shamelessly copied from https://github.com/motdotla/dotenv#comments:

# Comment
SECRET_KEY=YOURSECRETKEYGOESHERE # Comment
SECRET_HASH="something-with-a-#-hash"

Solution 3 - node.js

> You can add a comment in .env by starting a line with a hash (#) > symbol. E.g.

# host value
DB_HOST=host
# username
DB_USER=admin
# secure password
DB_PASS=pass

Solution 4 - node.js

Everything written in the same code line right of # or ; is comments.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionslideshowp2View Question on Stackoverflow
Solution 1 - node.jsValentine ShiView Answer on Stackoverflow
Solution 2 - node.jsmarko-36View Answer on Stackoverflow
Solution 3 - node.jssamranView Answer on Stackoverflow
Solution 4 - node.jsיצחק שליסלView Answer on Stackoverflow