Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
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?

about 4 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

about 4 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

about 4 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.

about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!