2

I am learning how to configure my Node.js App environment. For this I am using config module.

Below is my index.js file:

`
    const config=require('config');
    const express=require('express');
    const app=express();
    app.use(express.json()); //BUILT-IN EXPRESS MIDDLEWARE-FUNCTION

    //CONFIGURATION

    console.log('Current Working Environment:',process.env.NODE_ENV);
    console.log('Name is:', config.get('name'));
    console.log('Server is:', config.get('mail.host'));
    console.log('Password is:', config.get('mail.password'));

`

I set NODE_ENV to production by the power shell command: $env:NODE_ENV="production".

My production.json file inside the config folder is:

`{
    "name":"My Productoin Environmet",
    "mail":{
        "host": "Prod-Environment" 
    }
}` 

And custom-environment-variables.json file is:

`{
    "mail":{
        "passwrod":"app_password"
    }
}`

I set app_password to 12345678 by the power shell command : $env:app_password="12345678" config.get() is supposed to look at various sources to look for this configurations including, json files, configuration files and also environment variables. But whenever I run my app, I get the following error:

`throw new Error('Configuration property "' + property + '" is not defined'); Error: Configuration property "mail.password" is not defined`

If I remove the line : console.log('Password is:', config.get('mail.password')); everything goes well. Please, guide me what is the solution?

3
  • 2
    You probably misspell: 'passwrod' instead of 'password'. Commented Aug 24, 2018 at 11:53
  • @HeroQu, I am felling so embarrassed. Thank You :) Commented Aug 24, 2018 at 12:01
  • Take it easy! I do the same all the time... Commented Aug 24, 2018 at 12:05

5 Answers 5

2

Firstly you have a lot of syntactical errors for example in custom-environment-variables.json

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

Now if u need to store the password of your mail server in the environment variables

On windows

$env:app_password=12345

On Linux and OSX:

export app_password=12345

how to run ? app.js

const config = require("config");
console.log("Mail Password: " + config.get("mail.password"));

enter image description here

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

Comments

0

i had the same problem because i didn't define an environment variable for storing the password of the mail server. So, my suggestion will be define your environment variable for storing the password using the below command line (mac) and then your code should work.

export app_password=/* the password you want to set */

how to define an environment variable for storing the password of the mail server.

Comments

0

While defining environment variables in command_prompt don't put space on either side of '=' sign.....

eg:

set app_password = 123456 -----> is wrong way
   
set app_password=123456 ----->   will work

Comments

0

The issue in 99% of cases is in the name of the file in the config folder, storing your custom variables

Comments

0

To add on: make sure your file has .json extension.

Comments

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.