Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5049

NodeJS - 'config' is not able read form configuration files

$
0
0

I've been trying to set the environment variables using 'dotenv' pkg.

const dotenv = require('dotenv');dotenv.config();

and this is my .env file,

NODE_ENV=developmentPORT=3001NES_POSTGRES_DB=***NES_POSTGRES_HOST=***NES_POSTGRES_USER=***NES_POSTGRES_PASSWORD=***NES_POSTGRES_PORT=***NES_POSTGRES_REGION=***

Working fine. BUT the problem is, 'config' pkg is not able to read these.Following is my 'custom-environment-variables.json' file under the config directory of my project.

{"databases":{"test":{"database": "NES_POSTGRES_DB","host": "NES_POSTGRES_HOST","username": "NES_POSTGRES_USER","password": "NES_POSTGRES_PASSWORD","port": "NES_POSTGRES_PORT","dialect": "postgres"        }    },"node_port": "PORT"}

Now, using the config.get function to access the environment variable PORT (which was earlier set by dotenv, and it is accessible by process.env.PORT)

const port = config.get('node_port'); 

Error: Configuration property "PORT" is not defined.

Following is my server.js file,

const dotenv = require('dotenv');const config = require('config');const helmet = require('helmet');const express = require('express');const morgan = require('morgan');const fs = require('fs');const path = require('path');const routes = require('./routes/index');console.log(`Before dotenv.config() call -> NODE_ENV : ${process.env.NODE_ENV}`); // undefineddotenv.config();console.log(`After dotenv.config() call -> NODE_ENV : ${process.env.NODE_ENV}`); // 3001const expressApp = express();expressApp.use(helmet());expressApp.use(morgan('tiny'));const port = config.get('node_port'); expressApp.listen(port, () => {    console.log(`Express App (NES) is running on port: ${port}`);});

And following is my folder structure.

Project folder structure

Any sort of help will be much appreciated. Thanks in advance!


Viewing all articles
Browse latest Browse all 5049

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>