Unable to load env variables in local
I'm starting again with deno since a long time with a small telegram bot using the
grammy
framework, and since i need an API key i have to store it in environment variables. Those .env files are stored inside the src/environment folder, and when i call the function loadSync
from the dotenv library in deno.land/std
with export=true
and the correct environment file paths it does not load them. In fact I've also tried seeing if the object that loadSync would return contained any of my variables and it's completely empty.
I'm really confused as a long time ago i never had a slight problem using the dotenv library. I've also put the --allow-all
flag before running the process and it just doesn't read them. Just to be more explicit this is the command I'm running: deno run --allow-all --watch src/index.ts
where index.ts is the entry point of the application.3 Replies
I've also tried using the
load.ts
file from the dotenv std library and nothing changed.I'm sorry to hear that you're having trouble with loading environment variables in Deno. Based on the documentation, there are a few things we can check to troubleshoot this issue.
Firstly, ensure that you're using the correct path to your
.env
file when calling the loadSync
function. The path should be relative to the current working directory. If your .env
file is located in the src/environment
directory, you should be able to load it with something like this:
Secondly, make sure that your .env
file is formatted correctly. Each line should be a key-value pair in the format KEY=VALUE
. For example:
Lastly, ensure that you're running your Deno script with the --allow-env
flag. This flag is required to grant Deno permission to access environment variables. Since you're using the --allow-all
flag, this should already be covered, but it's worth double-checking.
If you're still having trouble after checking these things, it might be helpful to add some debugging output to your script to help identify the issue. For example, you could log the entire env
object after calling loadSync
to see if it contains the expected values:
If the env
object is empty, this suggests that loadSync
is not able to read your .env
file. In this case, you might want to check the file's permissions to ensure that Deno is able to read it.
I hope this helps! Let me know if you have any other questions. 🦕The issue was that i didn't know that the path was relative to the CWD. Now that I've actually tried changing it to what has been suggested it works. I'm almost embarassed by this.