jose
jose10mo ago

Environment Variable always returns undefined

I'm just trying to import the values from dotenv to the code, but it always returns undefined. deno version: 1.39.1 running command: deno run --allow-read --allow-env main.ts dotenv content:
FOO=123
FOO=123
main.ts content:
import "https://deno.land/std@0.208.0/dotenv/load.ts";
console.log(Deno.env.get("FOO"));
import "https://deno.land/std@0.208.0/dotenv/load.ts";
console.log(Deno.env.get("FOO"));
I also tried
import { load } from "https://deno.land/std@0.210.0/dotenv/mod.ts";

await load({
export:true,
envPath: "./.env",
allowEmptyValues: false,
});

const test = Deno.env.get("TEST_TOKEN")
console.log(test)
import { load } from "https://deno.land/std@0.210.0/dotenv/mod.ts";

await load({
export:true,
envPath: "./.env",
allowEmptyValues: false,
});

const test = Deno.env.get("TEST_TOKEN")
console.log(test)
result is always:
Task start deno run --allow-read --allow-env main.ts
undefined
Task start deno run --allow-read --allow-env main.ts
undefined
Update: also tried deno run --env --allow-env main.ts and it still is undefined.
14 Replies
iuioiua
iuioiua10mo ago
Firstly, your .env file is formatted incorrectly. It should be FOO=123. Secondly, use the —env CLI argument instead of load(). I.e. deno run —env main.ts
jose
jose10mo ago
Oh sorry, I writed it wrongly on the message, actually the file have = instead of : Still importing dotenv in main.ts?
iuioiua
iuioiua10mo ago
Deno Blog
Deno 1.38: HTML doc generator and HMR
Deno 1.38 ships with HTML doc output, hot module replacement, improved Node.js compatibility by allowing you to use your own node_modules folder, and more.
jose
jose10mo ago
Thankss somehow, still getting undefined
iuioiua
iuioiua10mo ago
Are you able to share the repo?
jose
jose10mo ago
yes, ill put it in github just a sec
jose
jose10mo ago
not a second lmao but here it is https://github.com/nogalogg/denotest
GitHub
GitHub - nogalogg/denotest
Contribute to nogalogg/denotest development by creating an account on GitHub.
Antonio Sampaio
Antonio Sampaio10mo ago
youre trying to get TEST_TOKEN instead of FOO
jose
jose10mo ago
omg 😭
iuioiua
iuioiua10mo ago
LOL! Yes, didn’t see that. I had a feeling it was a silly typo lol
Antonio Sampaio
Antonio Sampaio10mo ago
and.... you're trying to log test instead of testgi
jose
jose10mo ago
oh that i must've writed when i was configuring git to publish it finally it worked lmaoo
Antonio Sampaio
Antonio Sampaio10mo ago
<:deno_it_works:1181937887458111508> <:deno_thumbs_up:1181937842390319184>
jose
jose10mo ago
thank you sm