Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add comments to .env file?

Tags:

node.js

dotenv

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?

like image 841
slideshowp2 Avatar asked Jan 25 '19 04:01

slideshowp2


People also ask

How do I comment multiple lines in a .env file?

As you said, the only way to comment is at line level with the # character. A workaround would be looking for a shortcut in your IDE to employ multi-line editing (e.g. use Shift + Alt + Arrow Keys on Visual Studio Code in Linux).

Can I use variables in .env file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.

What should .env files contain?

env file should be particular to the environment and not checked into version control. This. env. example file documents the application's necessary variables and can be committed to version control.


2 Answers

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.

like image 73
Valentine Shi Avatar answered Oct 02 '22 19:10

Valentine Shi


The problem is the different line break.

like image 30
Anilson Lopes Avatar answered Oct 02 '22 19:10

Anilson Lopes