Exported TS module is undefined after import

39 views Asked by At

After importing my config module into ./index.ts config is always undefined. I am running around in circles trying to figure this out. Any suggestions are much appreciated.

Cheers!

Here is my file structure:

config/
    index.ts
    development.ts
    testing.ts
    production.ts

index.ts

code in config/index.ts:

    import { config as developmentConfig } from './development';
    import { config as testingConfig } from './testing';
    import { config as productionConfig } from './production';
    
    interface Config {
      host?: string;
      port?: number;
      dbName?: string;
      dbUser?: string;
      dbPassword?: string;
      databaseUrl?: string;
    }
    const env: string = process.env.NODE_ENV || 'development';
    
    const configuration: { [key: string]: Config } = {
      development: developmentConfig,
      testing: testingConfig,
      production: productionConfig,
    }
    
    export const config: Config = configuration[env] || {}

code in config/development.ts:

    import * as dotenv from "dotenv";
    dotenv.config();

    export const config = {
      host: process.env.HOST || 'localhost',
      port: parseInt(process.env.PORT!, 10) || 3000,
      dbName: process.env.NAME || 'my-app',
      dbUser: process.env.USER || 'my-username',
      dbPassword: process.env.PASSWORD || '',
      databaseUrl: process.env.DATABASE_URL || 'mongodb://localhost:27017/my-app',
    };

config/testing.ts and config/production.ts are pretty much the same as config/development.ts

code in ./index.ts:

    import pool from './src/pool';
    import app from './src/app';
    import { config } from './config';
        
    console.log(config.databaseUrl); // config is undefined
    console.log(config.dbName);
    console.log(config.dbPassword);
    console.log(config.dbUser);
    console.log(config.host);
    console.log(config.port);
    
    const host = config.host;
    const port = config.port;
    const database = config.dbName;
    const user = config.dbUser;
    const password = config.dbPassword;
    
    pool
        .connect({
            host,
            port,
            database,
            user,
            password
        })
        .then(() => {
            app().listen(3005, () => {
                console.log('Listening on port 3005');
            });
        })
        .catch((err) => console.error(err));

@katniss

tsconfig.json is as follows

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "target": "es6",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
            "*": ["node_modules/*"]
        }
    },
    "include": ["src/**/*", "index.ts"]
}
1

There are 1 answers

0
Rafael Fernandes On

Your code looks good, i think the problem maybe is this

const env: string = process.env.NODE_ENV || 'development';

If you set a NODE_ENV like 'testting' (wrong writing), this doesn't fallback to 'development' and will try to find a key that doesn't exist. It is the only thing I see at this point.