What is @npmcli/config?
@npmcli/config is a configuration management library for Node.js applications, particularly designed to handle npm's configuration needs. It allows you to load, manage, and manipulate configuration settings from various sources such as environment variables, command-line arguments, and configuration files.
What are @npmcli/config's main functionalities?
Loading Configuration
This feature allows you to load configuration settings from various sources. The `load` method initializes the configuration by reading from environment variables, command-line arguments, and configuration files.
const { Config } = require('@npmcli/config');
const config = new Config();
config.load().then(() => {
console.log(config.get('someKey'));
});
Setting Configuration
This feature allows you to set configuration values programmatically. The `set` method is used to assign a value to a specific configuration key.
const { Config } = require('@npmcli/config');
const config = new Config();
config.set('someKey', 'someValue');
console.log(config.get('someKey'));
Saving Configuration
This feature allows you to save the current configuration state back to the configuration file. The `save` method writes the current configuration settings to the appropriate file.
const { Config } = require('@npmcli/config');
const config = new Config();
config.set('someKey', 'someValue');
config.save().then(() => {
console.log('Configuration saved!');
});
Other packages similar to @npmcli/config
config
The `config` package is a popular configuration management library for Node.js applications. It allows you to define configuration settings for different deployment environments and load them easily. Compared to @npmcli/config, it is more general-purpose and not specifically tailored for npm's configuration needs.
dotenv
The `dotenv` package loads environment variables from a `.env` file into `process.env`. It is simpler and more lightweight compared to @npmcli/config, focusing solely on environment variable management rather than a comprehensive configuration management solution.
rc
The `rc` package is a non-opinionated configuration loader for Node.js. It supports loading configuration from various sources like environment variables, command-line arguments, and configuration files. It is similar to @npmcli/config but is more general-purpose and not specifically designed for npm.
@npmcli/config
Configuration management for the npm cli.
This module is the spiritual decendant of
npmconf
, and the code that once lived in npm's
lib/config/
folder.
It does the management of configuration files that npm uses, but
importantly, does not define all the configuration defaults or types, as
those parts make more sense to live within npm itself.
The only exceptions:
- The
prefix
config value has some special semantics, setting the local
prefix if specified on the CLI options and not in global mode, or the
global prefix otherwise. - The
project
config file is loaded based on the local prefix (which can
only be set by the CLI config options, and otherwise defaults to a walk
up the folder tree to the first parent containing a node_modules
folder, package.json
file, or package-lock.json
file.) - The
userconfig
value, as set by the environment and CLI (defaulting to
~/.npmrc
, is used to load user configs. - The
globalconfig
value, as set by the environment, CLI, and
userconfig
file (defaulting to $PREFIX/etc/npmrc
) is used to load
global configs. - A
builtin
config, read from a npmrc
file in the root of the npm
project itself, overrides all defaults.
The resulting heirarchy of configs:
- CLI switches. eg
--some-key=some-value
on the command line. - Environment variables. eg
npm_config_some_key=some_value
in the
environment. - INI-formatted project configs. eg
some-key = some-value
in ./.npmrc
- INI-formatted userconfig file. eg
some-key = some-value
in ~/.npmrc
- INI-formatted globalconfig file. eg
some-key = some-value
in
/usr/local/etc/npmrc
- INI-formatted builtin config file. eg
some-key = some-value
in
/usr/local/lib/node_modules/npm/npmrc
. - Default values (passed in by npm when it loads this module).
USAGE
const Config = require('@npmcli/config')
const types = require('./config/types.js')
const typeDefs = require('./config/type-defs.js')
const defaults = require('./config/defaults.js')
const shorthands = require('./config/shorthands.js')
const conf = new Config({
npmPath: resolve(__dirname, '..'),
types,
typeDefs,
shorthands,
defaults,
argv: process.argv,
env: process.env,
execPath: process.execPath,
platform: process.platform,
cwd: process.cwd(),
log: require('npmlog')
})
conf.load().then(() => {
console.log('loaded ok! some-key = ' + conf.get('some-key'))
}).catch(er => {
console.error('error loading configs!', er)
})