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'.
ts import nodeConfig from 'config'; interface Config { /** Whether assets should be cached or not. */ cache: boolean; /** The port that the express server should bind to. */ port: string; } const config: Config = { cache: nodeConfig. get<boolean>('cache'), port: nodeConfig.
You can run typescript directly on the node with the ts-node package. This package is recommended for development only. To make the final deploy in production, always use the javascript version of your project. The ts-node is already included as a dependency on another package, t ts-node-dev .
Node-config allows you to create configuration files in your Node application for different deployment environments. With it, you can define a default configuration file that you intend to repeat across environments, then extend the default config to other environments, such as development, staging, etc.
I'm taking a slightly different approach - defining the variables in JavaScript, and accessing them in TypeScript.
Using the following folder structure:
├── config │ ├── custom-environment-variables.js │ ├── default.js │ ├── development.js │ └── production.js └── server ├── config.ts └── main.ts
I define the configuration in the root config/
folder. For example:
// config/default.js module.exports = { cache: false, port: undefined // Setting to undefined ensures the environment config must define it }; // config/development.js module.exports = { port: '3000' } // config/production.js module.exports = { cache: true } // config/custom-environment-variables.js module.exports = { port: 'PORT' }
Now, in TypeScript land, I define an interface to provide nicer autocomplete & documentation, and write some bridging code to pull in the config from node-config
into my config map:
// server/config.ts import nodeConfig from 'config'; interface Config { /** Whether assets should be cached or not. */ cache: boolean; /** The port that the express server should bind to. */ port: string; } const config: Config = { cache: nodeConfig.get<boolean>('cache'), port: nodeConfig.get<string>('port') }; export default config;
Finally, I can now import and use my config variables inside any TypeScript code.
// server/main.ts import express from 'express'; import config from './config'; const { port } = config; const app = express(); app.listen(port);
This approach has the following benefits:
node-config
without needing to re-invent the wheelconfig.get
utility can be used to get the config values like so:
import config from 'config'; const port: number = config.get('server.port');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With