简体   繁体   English

配置未加载并出现错误-> configuration-property-MAX_CONNECTIONS-is-not-defined

[英]Config is not loading & getting-error -> configuration-property-MAX_CONNECTIONS-is-not-defined

I have a node.js app & have some configurations based on the environment.我有一个node.js应用程序并根据环境进行了一些配置。 So decided to use所以决定使用

https://www.npmjs.com/package/config https://www.npmjs.com/package/config

In accordance, I created a folder named config at the root level of the app.据此,我在应用程序的根级别创建了一个名为config的文件夹。 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这是我的本地。json & default.json

{
 "MAX_CONNECTIONS" : 100
}

in product.ts在 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错误-配置-属性-MAX_CONNECTIONS-未定义

Things tried尝试过的事情

  • uninstalled config & @types/config卸载的配置和@types/config
  • reinstalled config & @types/config重新安装配置和@types/config
  • moved the config folder inside src将配置文件夹移到 src 中
  • rebooted the app重新启动应用程序
  • cross checked the filename & the variable name交叉检查文件名和变量名

even then this is throwing the error即使这样,这也会引发错误

Thanks!谢谢!

You need to import config object and use config.get to read configuration value.您需要导入配置 object 并使用 config.get 读取配置值。 When you use import { get } from 'config';当您使用import { get } from 'config'; config library resolves incorrect this variable and therefore can't read the value. config 库解析不正确的this变量,因此无法读取该值。

import config from 'config';
// code emitted for brievty 
const allowedConnectionsLimit = config.get<number>('MAX_CONNECTIONS');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM