Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda