85

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?

Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • 3
    From [the source code](https://github.com/motdotla/dotenv/blob/master/lib/main.js) you can see that there is no support for comments or even blank lines. – Raymond Chen Jan 25 '19 at 04:27
  • Don't use comments. Can't u use GROUP=config? Or NOTGOINGTOUSETHISKEY = credentials? – Wimanicesir Feb 13 '19 at 11:20

5 Answers5

145

In 2022 both separate line comments and inline comments are supported.

Line started with # symbol is a separate line comment. See the docs. Inlined # sign denotes the start of an inline comment (thanks to @reddisht to note this in comments).

For vlucas/phpdotenv the same situation.

Here is the example for both:

# This is the separate comment line
NODE_ENV=stage
APP_VERSION=1.0.0 # This is an inline comment

The "#" (double quote wrapped hash symbol) is not treated as a comment even at line beginning starting from v15.0.0 (thanks to @walkingbrad commented this below).

There are parsing peculiarities you may find good to know described in this docs section.

Yet do not forget that some packages like e.g. mrsteele/dotenv-webpack (at least v7.1.1) do not support inline comments and you can face your application's unexpected behaviour putting inline comments in your .env files.

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
Valentine Shi
  • 6,604
  • 4
  • 46
  • 46
5

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"
marko-36
  • 1,309
  • 3
  • 23
  • 38
2

As of 13 Aug 2022 7:20 am UTC (Because Node, Angular, javascript etc. keep changing often) this is the status:

You can use # for comments.

# MY_TEMPORARY_VARIABLE = 'Some value'

But remember, this feature is still in primitive stage as it will not accept comment in same line. Thus:

MY_TEMPORARY_VARIABLE = 'Some value' # This is comment

In this case

let myVar = process.env.MY_TEMPORARY_VARIABLE;

myVar will hold value:

'Some value # This is comment'

You are welcome!

Atul
  • 3,778
  • 5
  • 47
  • 87
1

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
samran
  • 581
  • 6
  • 7
-2

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

Y.S.
  • 157
  • 1
  • 11