简体   繁体   中英

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

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=development

PORT=3001

NES_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}`); // undefined
dotenv.config();
console.log(`After dotenv.config() call -> NODE_ENV : ${process.env.NODE_ENV}`); // 3001

const 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.

项目文件夹结构

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

I would definitely agree with aitchkhan, using two separate packages for config management may not be best practice. Without using the config package, you can access the environment variables you set up with dotenv using process.env like so:

In .dotenv:

NODE_ENV=development

PORT=3001

NES_POSTGRES_DB=***
NES_POSTGRES_HOST=***
NES_POSTGRES_USER=***
NES_POSTGRES_PASSWORD=***
NES_POSTGRES_PORT=***
NES_POSTGRES_REGION=***

And in server.js:

// import dotenv and express
const express = require('express');
const dotenv = require('dotenv');
enter code here
// initialize server
const expressApp = express();

// initialize dotenv
dontenv.config()

// access port from process.env after initialization
const port = process.env.PORT

// should now log the correct port number
expressApp.listen(port, () => {
    console.log(`Express App (NES) is running on port: ${port}`);
});

If you would prefer to continue using the config package, it looks like it looks for its default config at ./config/default.json (as mentioned in its quick start guide ).

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