简体   繁体   中英

Config.get() Not Getting Configuration From File: "custom-environment-variables.json" Nodejs, JavaScript

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?

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"));

在此处输入图像描述

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.

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

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

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

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