node.jstypescriptnode-config

How to use node-config in typescript?


After installing node-config and @types/config:

yarn add config
yarn add --dev @types/config

And adding config as described in lorenwest/node-config:

// default.ts
export default {
  server: {
    port: 4000,
  },
  logLevel: 'error',
};

When I am trying to use in my app:

import config from 'config';

console.log(config.server);

I am getting the error:

src/app.ts(19,53): error TS2339: Property 'server' does not exist on type 'IConfig'.

Solution

  • config.get utility can be used to get the config values like so:

    import config from 'config';
    
    const port: number = config.get('server.port');