Environment variables are great for hiding sensitive information about your Deno application. This can be API keys, passwords, or other data which shouldnât be visible to others. Thatâs why there exists the .env file, which you would have to create, to hide sensitive information. We will create this file and pass some sensitive information to it:
In your source code files, you can use this environment variable with the dotenv third party library:
The utility function returns an object with all the key/value pairs from the .env file. Now the information isnât exposed in the source code anymore, but only available in the environment variables file.
Once you start your Deno application, you should see a permission error showing up on the command line: âUncaught PermissionDenied: read access to â/Users/mydspr/Developer/Repos/deno-exampleâ, run again with the âallow-read flagâ . You can allow the access on environment variables with a permission flag in Deno:
Itâs important to note that the .env file shouldnât be shared in a public repository where everybody can see it. If you make your source code public, for example on GitHub, consider adding the .env file to a .gitignore file.