I made a simple nodejs CLI using commander to send API requests. I am on Windows. I want to run the CLI from anywhere by calling the app name without having to type node.js. I was able to do this using npm link
. I am using dotenv
for environment variables where I store the API key. However, it looks like the app only picks up the variables from my .env file when I run the app from within the app folder. I am able to run the app from anywhere, but when I use a CLI command that calls a function that accesses the .env file, I get an undefedined TypeError
. It only works if I run this command from within the app folder. Is there a way I can get this to work where I don't need to be in the app folder and can run CLI commands that access the .env file?
Asked
Active
Viewed 491 times
1

user4584963
- 2,403
- 7
- 30
- 62
-
Hi, dotenv will load your variables on startup: this means that the variables are available only within process.env object that provides information about *the current Node.js process*. How do you access the .env file? Because you will have all the env variables within process.env object – MatteoPHRE Jan 24 '22 at 15:56
-
The file is accessed as you suggest `process.env.KEY` – user4584963 Jan 24 '22 at 16:19
-
https://stackoverflow.com/a/69192183/14998487 This might help – A-Tech Jan 25 '22 at 11:40