I am basically copying and pasting a Fullstack (react/Node.js/Mysql) project from Youtube.
And I encountered a strange bug looks nowhere can find an answer
According to the video and all sources everywhere, that is the quotation marks in .env
file.
Initially I followed the usual way, quotation mark outside a string, and I can not connect to my database:
DB_HOST = "localhost"
DB_USER = "root"
It takes me ages until I tried something like, and it worked:
DB_HOST = localhost
DB_USER = root
I am wondering why this happened? all I found is it does not matter if there is quotation marks in the .env file
Thanks in advance
Alan Lu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
When you include quotes in your .env file, they actually become part of the string value itself – meaning your application receives the value "localhost"
(with quotes) instead of just localhost
. This can cause issues with database connection configurations since the quotes are treated as literal characters. Most database drivers and connection libraries expect clean values without quotation marks. That’s why removing the quotes worked for you – it provides the raw value that the database connection expects.
For best practices in .env files, it’s recommended to only use quotes when your values contain spaces or special characters that need to be preserved. For simple values like hostnames, usernames, and ports, you can safely omit the quotes. This approach ensures the values are passed to your application exactly as intended without any extra characters that could interfere with connections or configurations.
watertrainer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.