'How to add comments to .env file?

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?



Solution 1:[1]

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 2:[2]

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 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marko-36
Solution 2 samran
Solution 3 יצחק שליסל