'Configuration property "dialect" is not defined

I'm having trouble setting up a project with the config npm package.

My config.js looks like this:


require('dotenv').config();

const { DB_HOST, DB_USERNAME, DB_PASSWORD, SENDGRID_API_KEY } = process.env;
const env = process.env.NODE_ENV;

const config = {
 development: {
   username: DB_USERNAME,
   password: DB_PASSWORD,
   database: "database_development",
   host: DB_HOST,
   dialect: "postgres",
   sendgrid: {
     base_url: 'https://localhost:3002',
     sendgrid_api_key: SENDGRID_API_KEY,
     sender_email: '',
     enabled: true,
   },
 },
 test: {
   username: DB_USERNAME,
   password: DB_PASSWORD,
   database: "database_test",
   host: DB_HOST,
   dialect: "postgres",
   sendgrid: {
     base_url: 'https://localhost:3002',
     sendgrid_api_key: SENDGRID_API_KEY,
     sender_email: '',
     enabled: true,
   },
 },
 production: {
   username: DB_USERNAME,
   password: DB_PASSWORD,
   database: "database_production",
   host: DB_HOST,
   dialect: "postgres",
   sendgrid: {
     base_url: 'https://localhost:3002',
     sendgrid_api_key: SENDGRID_API_KEY,
     sender_email: '',
     enabled: true,
   },
 }
};

module.exports = config[env];

In 1 service driver file I have the following line:

const dialect = config.get('dialect');

I get the following error: "Error: Configuration property "dialect" is not defined".

I also tried using 'development.dialect' but that doesn't help either.

Is it possible that the require('config'); doesn't work?

In my Sequelize's index.js file I've got
const config = require(__dirname + '/../config/config.js'); and that seems to work fine.



Solution 1:[1]

This github repository provides a good example of using config package.
https://github.com/basarbk/tdd-nodejs
Notes:

  1. for diffrent configs, you must have a file named whatever you use for NODE_ENV. for example for "start": "cross-env NODE_ENV=production node index" create a file called production.js
    check config folder in root directory
  2. if you are using window you should use cross-env.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Shefaee