• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

191
Views
exporting consts from CommonJS app amd require them in another file

I want to export const config from config.js file in CommonJs app.

const config = {
    development: {
        client: 'pg',
        connection: {
            database: 'myDatabase',
            user: 'myUser',
            host: 'localhost',
            password: 'password',
            port: PORT,
            ssl: {
                rejectUnauthorized: false
            }
        },
        server: {
            host: '127.0.0.1',
            port: 'PORT2'
        }
    }

module.exports = config;

and in index.js I require that like

var env = process.env.NODE_ENV || 'development';
const config = require('./config')[env];

const knexDB = knex({
    client: config.client, 
    connection: {
        database: config.database,
        user: config.user,
        host: config.host,
        password: config.password,
        port: config.port,
        ssl: {
            rejectUnauthorized: false
        }
    }
});

But in the config file. IntelliSense recommends changing module.exports to ES export which I don't want to do and keep the app CommonJS. also, config object in index.js I have this error :

Property 'host' does not exist on type '{ development: { client: string; connection: { database: string; user: string; host: string; password: string; port: number; ssl: { rejectUnauthorized: boolean; }; }; server: { host: string; port: string; }; }; production: { ...; }; }'.ts(2339)

How can I export config from config.js?

almost 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You're getting the wrong property of config. It must be config.development.host . give up on the vscode requesting a CommonJS module.leave it alone. You also have 2 options more to configure your constant data.

  1. yarn add dotenv

  2. npm install config

almost 3 years ago · Juan Pablo Isaza Report

0

Check config file you Db details is inside config.development.connection but you are reading it from config.

const knexDB = knex({
    client: config.development.client, 
    connection: {
        database: config.development.connection.database,
        user: config.development.connection.user,
        host: config.development.connection.host,
        password: config.development.connection.password,
        port: config.development.connection.port,
        ssl: {
            rejectUnauthorized: false
        }
    }

But instead of this use config or env

almost 3 years ago · Juan Pablo Isaza Report

0

well, I found a way to work around it. I removed connection from development and it worked! just mention I used config.connection.client and config.development.connection.client or other variables in the connection but still not working in the index.js. so it looks like

const config = {
    development: {
        client: 'pg',
        database: 'myDatabase',
        user: 'myUser',
        host: 'localhost',
        password: 'password',
        port: PORT,
        ssl: {
            rejectUnauthorized: false
        }
        server: {
            host: '127.0.0.1',
            port: 'PORT2'
        }
    }

module.exports = config;

and in index.js I access that like

const configure = require('./config.js')[env];
const knexDB = knex({
    client: configure.client, 
    connection: {
        database: configure.database, 
        user: configure.user,
        host: configure.host,
        password: configure.password,
        port: configure.port,
        ssl: {
            rejectUnauthorized: false
        }
    }
});

thanks for everyone for thier contribution.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error