Node.js 如何在typescript中使用节点配置?
安装Node.js 如何在typescript中使用节点配置?,node.js,typescript,node-config,Node.js,Typescript,Node Config,安装节点配置和@types/config后: yarn add config yarn add --dev @types/config 并添加配置,如中所述: 当我尝试在我的应用程序中使用时: import config from 'config'; console.log(config.server); 我得到一个错误: src/app.ts(19,53): error TS2339: Property 'server' does not exist on type 'IConfig'.
节点配置
和@types/config
后:
yarn add config
yarn add --dev @types/config
并添加配置,如中所述:
当我尝试在我的应用程序中使用时:
import config from 'config';
console.log(config.server);
我得到一个错误:
src/app.ts(19,53): error TS2339: Property 'server' does not exist on type 'IConfig'.
config.get
实用程序可用于获取如下配置值:
import config from 'config';
const port: number = config.get('server.port');
我使用
IConfig
接口,所以我可以先设置配置路径:
import { IConfig } from 'config';
export function dosomething() {
process.env["NODE_CONFIG_DIR"] = 'path to config dir';
//using get
const config: IConfig = require("config");
const port = config.get('server.port');
console.log('port', port);
//using custom schema
const config2: { server: { port: number } } = require("config");
console.log('config2.server.port', config2.server.port);
}
//port 4000
//config2.server.port 4000
从上一次开始,我仍然遇到问题,
config
无法从default.ts
中找到server
键
下面是我如何使用npm配置模块。将导出默认值{
更新为导出=
:
// default.ts
export = {
server: {
port: 4000,
},
logLevel: 'error',
};
应用程序内的使用情况[相同]:
import config from 'config';
console.log(config.get('server'));
使用此“从'config'导入*作为配置”,而不是“从'config'导入配置”
config/development.json
{
"server": {
"port": 4000
}
}
并设置NODE_ENV=development
export NODE_ENV=development
注意:如果使用默认设置,则不需要设置此节点。我采用的方法略有不同,即在JavaScript中定义变量,并在TypeScript中访问它们 使用以下文件夹结构:
├── config
│ ├── custom-environment-variables.js
│ ├── default.js
│ ├── development.js
│ └── production.js
└── server
├── config.ts
└── main.ts
我在rootconfig/
文件夹中定义配置。例如:
//config/default.js
module.exports={
cache:false,
端口:未定义//设置为未定义可确保环境配置必须定义它
};
//config/development.js
module.exports={
端口:“3000”
}
//config/production.js
module.exports={
缓存:真
}
//config/custom-environment-variables.js
module.exports={
端口:“端口”
}
现在,在TypeScript land中,我定义了一个接口来提供更好的自动完成和文档,并编写了一些桥接代码,将配置从节点配置
拉入配置映射:
// 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;
这种方法有以下好处:
- 我们可以使用
提供的丰富且经过战斗测试的功能,而无需重新发明轮子节点配置
- 我们有一个强类型、有良好文档记录的配置映射,可以从TS代码中的任何位置导入和使用
任何
返回类型
const serverConfig: any = config.get('server');
这只是一个解决办法。仍在寻找更好的答案。这就是预期使用配置的方式。
get
和has
是使用它的实际原因。它提供了太多的魔力,无法正确键入。您可以扩展它的类型,如const-config:IConfig&MySchema=require('config')
或使用自定义d.ts键入,但您无法确定预期的属性是否存在,因为它们可以存在于一个配置中,而不存在于另一个配置中。不确定“更好的答案”是什么样子,但需要注意的是,config.get()
支持泛型。您可以执行config.get('server.port'))
在某些情况下,这可能会更干净一些。
// 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;
// server/main.ts
import express from 'express';
import config from './config';
const { port } = config;
const app = express();
app.listen(port);
const serverConfig: any = config.get('server');