Ask coding questions

← Back to all posts
.env files not recognised in a bash replit
TheBeast941 (0)

I discovered this in another repl, where I would create a .env file and attempt to access variables from it in index.js and it would return "undefined", then I would create a dotenv file and access the varibles from that and the variables would get returned just fine.

After wasting a few hours trying to get it to work, I created a new repl to test what would happen, and this was indeed the behaviour: from index.js using the module "dotenv" with a .env file would return undefined, and using any file other than .env would return the variable, as expected.

Is there any way for the index.js file to be able to read variables from the .env file? (Switching languages from bash to node.js is not an option since this completely breaks the script (the main repl) for some reason)

Comments
hotnewtop
Coder100 (16868)

read this: https://docs.repl.it/repls/secret-keys

also your keys can't have spaces in them

test="message"

try $test see what u get

Coder100 (16868)

It's called .env not dotenv silly!

Coder100 (16868)

also btw
why do you need to use .env? the keys are set by default. @TheBossNoob

TheBeast941 (0)

@Coder100 just realised i sent comments on my alt account,oops!
Anyways, I need .env because the repl above is not the main repl, I got another repl which has the same problem and i'm looking for a way around it so that no one else can see the variables being stored in the file

Coder100 (16868)

hm?
.env is hidden by default you know
i really recommend you look at my other post @TheBeast941

TheBeast941 (0)

@Coder100

Here are images showing what I'm trying to explain. First one is using the .env file as the variable file, in which executing the script returns undefined. Second one is using the dotenv file as the variable file, in which exdecuting the file returns the value stored in the variable.

Also, for some reason I have found a workaround to using .env files correctly without them returning as undefined, by simply creating a .replit file and setting the run button to execute "node .". No idea why it works, but it's good enough for me. Also saves me time from having to constantly execute the script from console due to using Javascript in Bash!

Coder100 (16868)

@TheBeast941 yes,
replit runs some before-run commands and the .env set is one of them.

TheBeast941 (0)

@Coder100 Ah, so running "node . " in console means the script won't see the .env file, but running the command with the run button allows the script to see the .env file?