Whenever it comes to securing the API keys or something that we don't want to be exposed to the public for our Open Source Project, we always tend towards the .env file, and every week, 29k+ developers download a trendy package, dotenv.
It's a myth, that the filename can only start with .env. You can name it anything and it will still be working fine with node.js.
For example, this is my folder structure to test this feature, and as you can see, instead of .env, I added my name as a filename.
This file contains a PORT, which I want to print in my main file
And, as you can see, the PORT is being printed in the console.
When it comes to environment files, it's considered good to use a dot ( . ), in front of the filename, because adding a dot in front of any file name, makes it a hidden file or folder.
That's why there are multiple folders in your OS, that are hidden and can only be accessed via CLI, For example, .ssh, .github, .vscode, etc.
Instead of using dotenv to read environment files, you can use the node.js inbuilt method to read them, which is
node --env-file=.env app.js
Here, instead of using .env as a file name, you can use any file name that is in your root folder.
The above is the detailed content of Stop Using dotenv file. For more information, please follow other related articles on the PHP Chinese website!