Is there a way to allow programs other than server.js to access process.env


#1

Hello,

I have a discord bot that uploads the daily Garfield comic every morning and I want to expand it to Twitter. To reduce clutter in the already extensive server.js file I have a new file twitter.js that I run through the console for now. However, twitter.js is unable to access keys and secrets using process.env, returning undefined.

Is there something I am missing when it comes to multiple files? Any help would be greatly appreciated.


#2

Hello, there.

Make sure the env fields are named correctly and used too.

Env file:
SECRET=abcdefghijklmnopqrstuvwxyz

For example:
client.login(process.env.SECRET);

If you don’t find it helpful, please private message/dm me.

Thanks,
Bolt.


#3

don’t you need quotes for strings?


#4

You can use both of them, I would recommend quotes if there are any number.


#5

In the .env I have

test=working

and accessing it with

process.env.test

returns undefined.

Accessing it in server.js correctly returns working.


#6

You can use a package like dotenv-safe and config it in your app file it adds all the env variables in the .env file into the process.env
link: https://github.com/rolodato/dotenv-safe


#7

I’d check your code, as this project shows you don’t need to specify the process.env value any differently when accessing environment variables in different files: https://glitch.com/edit/#!/join/2b95fc10-20b1-46b7-8d04-476612152a1e

If you still can’t figure it out, share your project name so we can see the code. If your project is private, either make it public temporarily or DM me a join link. Thanks.


#8

Did you mean to post a join link there?


#9

I did - it was just a quick example that shows it’s working with console.logs. Hopefully I can trust the community to not trash it in the time it takes @LiquidZulu to review it :slight_smile:


#10

Is twitter.js running on the client? If so, that’s the issue. You don’t want client-side JS to be able to see your secrets.


#11

Alright, I don’t know why but it is now working. I believe there must have been an issue with my ping as I am having networking problems at the moment. Thank you all for your help.