1

I use config library to manage my configuration files in node application.

I have export my custom env variable in terminal by export app_password=12345

and map it into the custom-environment-variable.json file by

{
  "mail": {
  "password": "app_password"
   }
}

when I use config.get('mail.password') in my index.js file (root module) .

I got error: Configuration property "mail.password" is not defined, detail in below:

/Users/tzhong/Documents/vidly- 
backend/node_modules/config/lib/config.js:203
throw new Error('Configuration property "' + property + '" is not 
defined');
^

Error: Configuration property "mail.password" is not defined
    at Config.get (/Users/tzhong/Documents/vidly- 
 backend/node_modules/config/lib/config.js:203:11)

I have use console.log(process.env.app_password) to check if my custom env variable is there, and the app_password show in the terminal.

 Vidly evn is : Vidly backend -- development
 Vidly mail server is : dev-mail-server
 Mail-password is : 12345
 NODE_ENV is : undefined
 app: development
 Mongan enabled...
 Listenning 3000 port ...

I want to use console.log(config.get('mail.password')); to show my custom variable in terminal instead of console.log(process.env.app_password) Any suggestions?

Finally solve the problem : It's tiny mistake, in config library, the file's name for creating custom environment variable is strict, the file's name should be 'custom-environment-variables.json', I have a wrong spell on 'environment' , after all , thanks for people offer other available package

0

4 Answers 4

3

Rename the filename to 'custom-environment-variables.json' with a plural "variables".

Sign up to request clarification or add additional context in comments.

Comments

1

Right way to do this in nodejs is to make use of .env file. Create a .env file at the root. Keep your env variables and their value there in the key=value format for e.g password=app_password

In your main entry point file, Require the dotenv package and load the env variables with dotenv.configure() at the top just after requiring all the modules. See here for more details.

2 Comments

Hey Rajesh, thanks for the suggestion, but do you know what wrong in my code when I using config library?
I have solve the problem, the path for config.get('mail.password') is okey, just the custom environment variables file name should be correct
0

The issue is with VS Code Terminal. It not picking the file or object. Try external node terminal. It will work.

Comments

-1

Instead of using config use .env. Just simply add by typing npm install --save dotenv

password=some value

You can access this by

require("dotenv").config();

console.log(`Password is ${process.env.password}.`);
//Output
Password is *some value*

1 Comment

hi namus, I think I can use console.log(process.env.password) without dotenv package, I want to do the same thing with config package.Thank for your comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.