What is @backstage/config?
@backstage/config is a configuration management library designed for use with Backstage, a platform for building developer portals. It provides a structured way to manage and access configuration data, supporting features like environment variable substitution, schema validation, and hierarchical configuration.
What are @backstage/config's main functionalities?
Loading Configuration
This feature allows you to load configuration data into a ConfigReader instance, which can then be used to access configuration values using a path-like syntax.
const { ConfigReader } = require('@backstage/config');
const config = new ConfigReader({
app: {
title: 'My App',
port: 3000
}
});
console.log(config.getString('app.title')); // Outputs: 'My App'
Environment Variable Substitution
This feature allows you to use environment variables within your configuration files, enabling dynamic configuration based on the environment.
const { ConfigReader } = require('@backstage/config');
process.env.APP_PORT = '4000';
const config = new ConfigReader({
app: {
port: '${APP_PORT}'
}
});
console.log(config.getNumber('app.port')); // Outputs: 4000
Schema Validation
This feature allows you to define a schema for your configuration and validate the configuration data against this schema, ensuring that the configuration is structured and typed correctly.
const { ConfigReader, ConfigSchema } = require('@backstage/config');
const schema = ConfigSchema.object({
app: ConfigSchema.object({
title: ConfigSchema.string(),
port: ConfigSchema.number()
})
});
const config = new ConfigReader({
app: {
title: 'My App',
port: 3000
}
});
schema.validate(config); // Validates the configuration against the schema
Other packages similar to @backstage/config
config
The 'config' package is a popular Node.js configuration management library that supports hierarchical configurations, environment variable overrides, and multiple configuration file formats. It is similar to @backstage/config in its ability to manage complex configurations but does not provide built-in schema validation.
dotenv
The 'dotenv' package is a zero-dependency module that loads environment variables from a .env file into process.env. While it is simpler and more focused on environment variable management compared to @backstage/config, it lacks the hierarchical and schema validation features.
convict
The 'convict' package provides a way to define a schema for configuration files, similar to @backstage/config. It supports environment variable overrides and validation, making it a close alternative for applications that require schema validation and structured configuration management.
@backstage/config
This package provides a config API used by Backstage core, backend, and CLI.
Installation
Do not install this package directly, it is an internal package used by @backstage/core, @backstage/cli, and @backstage/backend-common. Depend on either of those instead.
Documentation