I have a node.js
app & have some configurations based on the environment. So decided to use
https://www.npmjs.com/package/config
In accordance, I created a folder named config
at the root level of the app. The folder structure looks like as below
app
-config
--default.json
--local.json
--qa.json
--prod.json
-src
--routes
---products.ts
this is my local.json & default.json
{
"MAX_CONNECTIONS" : 100
}
in product.ts
import { get } from 'config';
// code emitted for brievty
const allowedConnectionsLimit = get<number>('MAX_CONNECTIONS'); //also tried with get("MAX_CONNECTIONS")
But this keeps on throwing the error
error-configuration-property-MAX_CONNECTIONS-is-not-defined
Things tried
even then this is throwing the error
Thanks!
You need to import config object and use config.get to read configuration value. When you use import { get } from 'config';
config library resolves incorrect this
variable and therefore can't read the value.
import config from 'config';
// code emitted for brievty
const allowedConnectionsLimit = config.get<number>('MAX_CONNECTIONS');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.