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


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.

1 Like

Hello, there.

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

Env file:

For example:

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


don’t you need quotes for strings?

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

In the .env I have


and accessing it with


returns undefined.

Accessing it in server.js correctly returns working.

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

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:!/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.

Did you mean to post a join link there?

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:

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.

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.