In bash I can see npm environment variables with npm run env
. USER=brianmackey
is one such environment variable. How can I set an environment variable, say USER=billybob
?
I know I can use npm config set <key> <value> [--global]
. Is key+value always/in any case an environment variable? Can I set the environment variables in session?
An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.
Single Command
If you want to set environment variables for a single node command, you can simply do this:
$ USER=billybob node server.js
Loaded for each session
If you want to permanently set that environment variable for your user, edit your ~/.bash_profile
and add the following line:
export USER="billybob"
This will automatically set the given environment variable each time you create a new terminal session.
Existing for the entire current session
Lastly, if you want to set the environment variable only for the current session, just run it as it's own command:
$ USER=billybob
$ node app.js # user is billybob
$ node app.js # user is still billybob
When you exit the session, these temporarily set environment variables will be cleared.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With