I am following a node course and while setting the environment variable as shown it does not work
It says to set environment variables, use SET
on Windows and EXPORT
on Mac:
set jwtkey=myprivatekey
, when I use this nothing happens, it does not set anything.
This is set using the config package, and I have also created a separate files for default.json
and custom-env.json
if (!config.get("jwtPrivateKey")) {
console.error("FATAL ERROR: JwtKey Not Set");
process.exit(1);
}
please help
this is my .env file(using dotenv)
PORT=9000 //this is working
jwtPrivateKey="myPrivateKey" //this is not working tho.
[basically, I wanted to hide my VERIFY SIGNATURE(digital signature) and store it in ENV so that it cant be accessed.]
CodePudding user response:
I would do it differently.
Create a
.env
file in your server root.Add the key/value pair:
JWT_PRIVATE_KEY="936392a8c..."
Install
dotenv
, and import it.Access
process.env.JWT_PRIVATE_KEY
from anywhere in your server app.
CodePudding user response:
If you use set in a Windows terminal, it will set the variable just for this terminal. If you start the node in the same terminal, everything should work fine. Otherwise read this article on how to change the variable permanently.
There are two types of environment variables in Windows: user and system variables. If possible, use a user variable.
You might want to read the answers to this similar question