简体   繁体   中英

Keep getting Error: Configuration property "vidly_jwtPrivateKey" is not defined

Error Screenshot [ I am using Node.js and have already set the enviroment varibale vidly_jwtPrivateKey. and try to retrieve it using config package using this command config.get('vidly_jwtPrivateKey')but i keep getting this error :

throw new Error('Configuration property "' + property + '" is not defined'); Error: Configuration property "vidly_jwtPrivateKey" is not defined,

i have been searching for hours , could you please suggest a solution.

here is what my default.json file contains :

{
    "jwtPrivateKey" : ""
}

and custom-environment-variables.json contain :

{
    "jwtPrivateKey" : "vidly_jwtPrivateKey"
}

and i have already set vidly_jwtPrivateKey by exporting it in terminal (I use a Mac):

export vidly_jwtPrivateKey=mySecureKey.

I have tried this , but it doesn't work :

Error: Configuration property "mongoURI" is not defined

Here is the code I am suing to retrieve the environment variable:

if (!config.get('vidly_jwtPrivateKey')){
  console.error('FATAL ERROR : jwtPrivateKey is not defined '),
  process.exit(1);
};

Thanks a lot

I've gone through the same problem it's because of the integrated terminal in visual studio and visual studio code it seems like they don't have access to any of these variables unless you run the editor in Admin mode.so to solve this problem you just need to start your editor in Amin mode

PS: what I can see on the answer is not correct because you actually you do not read it from the environment variable any more if you change it to

config.get('jwtPrivateKey')

I know what tutorial this code is related to. so it should be

config.get('vidly_jwtPrivateKey')

you should run your editor in ADMIN mode instead

You are trying to fetch the value not the key name by:

config.get('vidly_jwtPrivateKey')

It should be,

config.get('jwtPrivateKey')

I had the same issue, and according to the official documentation of the config package https://www.npmjs.com/package/config , you should use config.has() to test if a configuration value is defined. config.get() will throw an exception for undefined keys to help catch typos and missing

const config = require('config');
//...
 
if(!config.has('jwtPrivateKey')){
  console.error('FATAL ERROR: jwtPrivateKey is not defined');
  process.exit(1);
}

I encountered the same issue and was stuck trying to debug it for hours. In my opinion, you should totally switch to using the dotenv NPM package. It's a lightweight and better solution when it comes to storing any kind of secrets. You simply need to require it using

require('dotenv').config()

Or by typing this in your CLI

node -r dotenv/config your_script.js

Both work fantastically well.

Then, simply save your token in a .env file as

RANDOM_SECRET_TOKEN = someRandomSecretIDon'tcareAbout

You can use whatever you want as the secret

Finally just refer it wherever you want in your entire environment using

process.env.RANDOM_SECRET_TOKEN

In this case, it is used in this sense:

jwt.sign({_id : this._id}, process.env.RANDOM_ACCESS_TOKEN)

Trust me, using config only makes sense for altering configuration of certain variables as you change environments (eg development => testing => production). For storing secrets, dotenv package is easier and better

I tried using opening the VS Code as administrator, my environment variable is app_password. and I have stored it in a JSON file like this { "mail" : { "password" : "app_password" } }

and accessing the config by using config.get('mail.password')

{
  "jwtPrivateKey" : "vidly_jwtPrivateKey"
}

if (!config.get('vidly_jwtPrivateKey')) {
  // use config.get('jwtPrivateKey')
};

In your code, try to call get on the value instead of the key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM