2
votes

So I have install npm config module.

I have trying to set a local variable that stores a secret/privateKey.

Tried setting an environment variable/private key using the following command

 export VIDLY_JWTPRIVATEKEY=MYSECUREKEY

however it don't think It is being set as I get an error via

console.log("FATAL ERROR: JWTPRIVATEKEY is not defined");

This is how I am checking if the key is set..

index.js

if (!config.get("JWTPRIVATEKEY")) {
  console.log("FATAL ERROR: JWTPRIVATEKEY is not defined");
  // node environment variable. 1 (any other number exc. 0) is exit the app, 0 is success
  process.exit(1);
}

userAuth.js I once again try to get the private, however its not set (?)

   #code above 

  const token = jwt.sign({ _id: user._id }, config.get("JWTPRIVATEKEY"));

default.json (in config module folder)

{
  "JWTPRIVATEKEY": ""
}

custom-environment-variables.json (in config folder)

{
  "JWTPRIVATEKEY": "VIDLY_JWTPRIVATEKEY"
}

**

ERROR: "FATAL ERROR: JWTPRIVATEKEY is not defined"

**

What am I doing wrong?

2
I could not get if the question is about environment variables or config file. - vahdet
in config module folder? The json files should be in ./config/*.jsonalso if you want to use js export it like module.exports = {"JWTPRIVATEKEY": "VIDLY_JWTPRIVATEKEY"} - The docs are extensive github.com/lorenwest/node-config/wiki - Lawrence Cherone
No the config module is an npm package, and the file is suppose to be setup up like they are... i.e. no need to export module.... i reckon the local variable is not being set ? - Shaz
@vahdet updated the question for clarity - Shaz

2 Answers

0
votes

Instead of setting the variable with this way, you can try to set it in production and development config folders like this:

config/development.js

const config = {
    env: 'development',
    JWTPRIVATEKEY: <your_dev_key>
};

module.exports = config;

config/production.js

const config = {
    env: 'production',
    JWTPRIVATEKEY: <your_prod_key>
};

module.exports = config;
0
votes

it is two years later! but i think it can be useful, atleast for others. if you set environment variable AND run the app in IDE's Terminal, it will not work! but if do these in "cmd", it works.