Does nodejs give you a way to see all environment variables that it can use?
Asked Answered
G

1

8

When I start to use an existing app/codebase, I'm often confounded by which environment variables it's wired to use. People make bad docs, and I hate hunting and being disoriented by this part of app dev.

Is there a way to see all environment variables that it can use? Like an npm run... task that lists them all statically?

Groundnut answered 3/6, 2020 at 16:51 Comment(0)
S
11

Node.js can basically use all of your systems environment variables. try console.log(process.env) and you'll find that all environment variables that you've declared, even before starting your node app are showing.

You can also create new environment variables through node, by doing process.env.MY_ENVIRONMENT_VARIABLE = 'hello'. So i guess searching process.env through the whole project should list all env variables being accessed and created inside node.

Substage answered 3/6, 2020 at 17:41 Comment(4)
it's worth noting that this may not get all such usages, because another library could wrap the process.env and give you another API for env vars. But that's a different scope of issue, and I think your answer is reasonable enough.Groundnut
@NewAlexandria wouldn't that comment be relevant for any nodejs code? See fermatslibrary.com/s/reflections-on-trusting-trust (I'm a NodeJS newbie so maybe I'm missing something fundamental)Trimolecular
if I hear you, it's right in principle, but in practice here we limit the scope to 'any process.env calls' and so we could get all such instances by scanning the entire dependency tree for that string, and then limiting scope as possible from there. If you want to continue on chat.SE, I'm glad toGroundnut
You may need console.log(JSON.stringify(process.env))Host

© 2022 - 2024 — McMap. All rights reserved.