11cb0ef41Sopenharmony_ci# `@npmcli/config` 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciConfiguration management for the npm cli. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciThis module is the spiritual descendant of 61cb0ef41Sopenharmony_ci[`npmconf`](http://npm.im/npmconf), and the code that once lived in npm's 71cb0ef41Sopenharmony_ci`lib/config/` folder. 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciIt does the management of configuration files that npm uses, but 101cb0ef41Sopenharmony_ciimportantly, does _not_ define all the configuration defaults or types, as 111cb0ef41Sopenharmony_cithose parts make more sense to live within the npm CLI itself. 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciThe only exceptions: 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci- The `prefix` config value has some special semantics, setting the local 161cb0ef41Sopenharmony_ci prefix if specified on the CLI options and not in global mode, or the 171cb0ef41Sopenharmony_ci global prefix otherwise. 181cb0ef41Sopenharmony_ci- The `project` config file is loaded based on the local prefix (which can 191cb0ef41Sopenharmony_ci only be set by the CLI config options, and otherwise defaults to a walk 201cb0ef41Sopenharmony_ci up the folder tree to the first parent containing a `node_modules` 211cb0ef41Sopenharmony_ci folder, `package.json` file, or `package-lock.json` file.) 221cb0ef41Sopenharmony_ci- The `userconfig` value, as set by the environment and CLI (defaulting to 231cb0ef41Sopenharmony_ci `~/.npmrc`, is used to load user configs. 241cb0ef41Sopenharmony_ci- The `globalconfig` value, as set by the environment, CLI, and 251cb0ef41Sopenharmony_ci `userconfig` file (defaulting to `$PREFIX/etc/npmrc`) is used to load 261cb0ef41Sopenharmony_ci global configs. 271cb0ef41Sopenharmony_ci- A `builtin` config, read from a `npmrc` file in the root of the npm 281cb0ef41Sopenharmony_ci project itself, overrides all defaults. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciThe resulting hierarchy of configs: 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci- CLI switches. eg `--some-key=some-value` on the command line. These are 331cb0ef41Sopenharmony_ci parsed by [`nopt`](http://npm.im/nopt), which is not a great choice, but 341cb0ef41Sopenharmony_ci it's the one that npm has used forever, and changing it will be 351cb0ef41Sopenharmony_ci difficult. 361cb0ef41Sopenharmony_ci- Environment variables. eg `npm_config_some_key=some_value` in the 371cb0ef41Sopenharmony_ci environment. There is no way at this time to modify this prefix. 381cb0ef41Sopenharmony_ci- INI-formatted project configs. eg `some-key = some-value` in the 391cb0ef41Sopenharmony_ci `localPrefix` folder (ie, the `cwd`, or its nearest parent that contains 401cb0ef41Sopenharmony_ci either a `node_modules` folder or `package.json` file.) 411cb0ef41Sopenharmony_ci- INI-formatted userconfig file. eg `some-key = some-value` in `~/.npmrc`. 421cb0ef41Sopenharmony_ci The `userconfig` config value can be overridden by the `cli`, `env`, or 431cb0ef41Sopenharmony_ci `project` configs to change this value. 441cb0ef41Sopenharmony_ci- INI-formatted globalconfig file. eg `some-key = some-value` in 451cb0ef41Sopenharmony_ci the `globalPrefix` folder, which is inferred by looking at the location 461cb0ef41Sopenharmony_ci of the node executable, or the `prefix` setting in the `cli`, `env`, 471cb0ef41Sopenharmony_ci `project`, or `userconfig`. The `globalconfig` value at any of those 481cb0ef41Sopenharmony_ci levels can override this. 491cb0ef41Sopenharmony_ci- INI-formatted builtin config file. eg `some-key = some-value` in 501cb0ef41Sopenharmony_ci `/usr/local/lib/node_modules/npm/npmrc`. This is not configurable, and 511cb0ef41Sopenharmony_ci is determined by looking in the `npmPath` folder. 521cb0ef41Sopenharmony_ci- Default values (passed in by npm when it loads this module). 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci## USAGE 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci```js 571cb0ef41Sopenharmony_ciconst Config = require('@npmcli/config') 581cb0ef41Sopenharmony_ciconst { shorthands, definitions, flatten } = require('@npmcli/config/lib/definitions') 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciconst conf = new Config({ 611cb0ef41Sopenharmony_ci // path to the npm module being run 621cb0ef41Sopenharmony_ci npmPath: resolve(__dirname, '..'), 631cb0ef41Sopenharmony_ci definitions, 641cb0ef41Sopenharmony_ci shorthands, 651cb0ef41Sopenharmony_ci flatten, 661cb0ef41Sopenharmony_ci // optional, defaults to process.argv 671cb0ef41Sopenharmony_ci // argv: [] <- if you are using this package in your own cli 681cb0ef41Sopenharmony_ci // and dont want to have colliding argv 691cb0ef41Sopenharmony_ci argv: process.argv, 701cb0ef41Sopenharmony_ci // optional, defaults to process.env 711cb0ef41Sopenharmony_ci env: process.env, 721cb0ef41Sopenharmony_ci // optional, defaults to process.execPath 731cb0ef41Sopenharmony_ci execPath: process.execPath, 741cb0ef41Sopenharmony_ci // optional, defaults to process.platform 751cb0ef41Sopenharmony_ci platform: process.platform, 761cb0ef41Sopenharmony_ci // optional, defaults to process.cwd() 771cb0ef41Sopenharmony_ci cwd: process.cwd(), 781cb0ef41Sopenharmony_ci}) 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci// emits log events on the process object 811cb0ef41Sopenharmony_ci// see `proc-log` for more info 821cb0ef41Sopenharmony_ciprocess.on('log', (level, ...args) => { 831cb0ef41Sopenharmony_ci console.log(level, ...args) 841cb0ef41Sopenharmony_ci}) 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci// returns a promise that fails if config loading fails, and 871cb0ef41Sopenharmony_ci// resolves when the config object is ready for action 881cb0ef41Sopenharmony_ciconf.load().then(() => { 891cb0ef41Sopenharmony_ci conf.validate() 901cb0ef41Sopenharmony_ci console.log('loaded ok! some-key = ' + conf.get('some-key')) 911cb0ef41Sopenharmony_ci}).catch(er => { 921cb0ef41Sopenharmony_ci console.error('error loading configs!', er) 931cb0ef41Sopenharmony_ci}) 941cb0ef41Sopenharmony_ci``` 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci## API 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ciThe `Config` class is the sole export. 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci```js 1011cb0ef41Sopenharmony_ciconst Config = require('@npmcli/config') 1021cb0ef41Sopenharmony_ci``` 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci### static `Config.typeDefs` 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ciThe type definitions passed to `nopt` for CLI option parsing and known 1071cb0ef41Sopenharmony_ciconfiguration validation. 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci### constructor `new Config(options)` 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ciOptions: 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci- `types` Types of all known config values. Note that some are effectively 1141cb0ef41Sopenharmony_ci given semantic value in the config loading process itself. 1151cb0ef41Sopenharmony_ci- `shorthands` An object mapping a shorthand value to an array of CLI 1161cb0ef41Sopenharmony_ci arguments that replace it. 1171cb0ef41Sopenharmony_ci- `defaults` Default values for each of the known configuration keys. 1181cb0ef41Sopenharmony_ci These should be defined for all configs given a type, and must be valid. 1191cb0ef41Sopenharmony_ci- `npmPath` The path to the `npm` module, for loading the `builtin` config 1201cb0ef41Sopenharmony_ci file. 1211cb0ef41Sopenharmony_ci- `cwd` Optional, defaults to `process.cwd()`, used for inferring the 1221cb0ef41Sopenharmony_ci `localPrefix` and loading the `project` config. 1231cb0ef41Sopenharmony_ci- `platform` Optional, defaults to `process.platform`. Used when inferring 1241cb0ef41Sopenharmony_ci the `globalPrefix` from the `execPath`, since this is done diferently on 1251cb0ef41Sopenharmony_ci Windows. 1261cb0ef41Sopenharmony_ci- `execPath` Optional, defaults to `process.execPath`. Used to infer the 1271cb0ef41Sopenharmony_ci `globalPrefix`. 1281cb0ef41Sopenharmony_ci- `env` Optional, defaults to `process.env`. Source of the environment 1291cb0ef41Sopenharmony_ci variables for configuration. 1301cb0ef41Sopenharmony_ci- `argv` Optional, defaults to `process.argv`. Source of the CLI options 1311cb0ef41Sopenharmony_ci used for configuration. 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ciReturns a `config` object, which is not yet loaded. 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ciFields: 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci- `config.globalPrefix` The prefix for `global` operations. Set by the 1381cb0ef41Sopenharmony_ci `prefix` config value, or defaults based on the location of the 1391cb0ef41Sopenharmony_ci `execPath` option. 1401cb0ef41Sopenharmony_ci- `config.localPrefix` The prefix for `local` operations. Set by the 1411cb0ef41Sopenharmony_ci `prefix` config value on the CLI only, or defaults to either the `cwd` or 1421cb0ef41Sopenharmony_ci its nearest ancestor containing a `node_modules` folder or `package.json` 1431cb0ef41Sopenharmony_ci file. 1441cb0ef41Sopenharmony_ci- `config.sources` A read-only `Map` of the file (or a comment, if no file 1451cb0ef41Sopenharmony_ci found, or relevant) to the config level loaded from that source. 1461cb0ef41Sopenharmony_ci- `config.data` A `Map` of config level to `ConfigData` objects. These 1471cb0ef41Sopenharmony_ci objects should not be modified directly under any circumstances. 1481cb0ef41Sopenharmony_ci - `source` The source where this data was loaded from. 1491cb0ef41Sopenharmony_ci - `raw` The raw data used to generate this config data, as it was parsed 1501cb0ef41Sopenharmony_ci initially from the environment, config file, or CLI options. 1511cb0ef41Sopenharmony_ci - `data` The data object reflecting the inheritance of configs up to this 1521cb0ef41Sopenharmony_ci point in the chain. 1531cb0ef41Sopenharmony_ci - `loadError` Any errors encountered that prevented the loading of this 1541cb0ef41Sopenharmony_ci config data. 1551cb0ef41Sopenharmony_ci- `config.list` A list sorted in priority of all the config data objects in 1561cb0ef41Sopenharmony_ci the prototype chain. `config.list[0]` is the `cli` level, 1571cb0ef41Sopenharmony_ci `config.list[1]` is the `env` level, and so on. 1581cb0ef41Sopenharmony_ci- `cwd` The `cwd` param 1591cb0ef41Sopenharmony_ci- `env` The `env` param 1601cb0ef41Sopenharmony_ci- `argv` The `argv` param 1611cb0ef41Sopenharmony_ci- `execPath` The `execPath` param 1621cb0ef41Sopenharmony_ci- `platform` The `platform` param 1631cb0ef41Sopenharmony_ci- `defaults` The `defaults` param 1641cb0ef41Sopenharmony_ci- `shorthands` The `shorthands` param 1651cb0ef41Sopenharmony_ci- `types` The `types` param 1661cb0ef41Sopenharmony_ci- `npmPath` The `npmPath` param 1671cb0ef41Sopenharmony_ci- `globalPrefix` The effective `globalPrefix` 1681cb0ef41Sopenharmony_ci- `localPrefix` The effective `localPrefix` 1691cb0ef41Sopenharmony_ci- `prefix` If `config.get('global')` is true, then `globalPrefix`, 1701cb0ef41Sopenharmony_ci otherwise `localPrefix` 1711cb0ef41Sopenharmony_ci- `home` The user's home directory, found by looking at `env.HOME` or 1721cb0ef41Sopenharmony_ci calling `os.homedir()`. 1731cb0ef41Sopenharmony_ci- `loaded` A boolean indicating whether or not configs are loaded 1741cb0ef41Sopenharmony_ci- `valid` A getter that returns `true` if all the config objects are valid. 1751cb0ef41Sopenharmony_ci Any data objects that have been modified with `config.set(...)` will be 1761cb0ef41Sopenharmony_ci re-evaluated when `config.valid` is read. 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci### `config.load()` 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ciLoad configuration from the various sources of information. 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ciReturns a `Promise` that resolves when configuration is loaded, and fails 1831cb0ef41Sopenharmony_ciif a fatal error is encountered. 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci### `config.find(key)` 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ciFind the effective place in the configuration levels a given key is set. 1881cb0ef41Sopenharmony_ciReturns one of: `cli`, `env`, `project`, `user`, `global`, `builtin`, or 1891cb0ef41Sopenharmony_ci`default`. 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ciReturns `null` if the key is not set. 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ci### `config.get(key, where = 'cli')` 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ciLoad the given key from the config stack. 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci### `config.set(key, value, where = 'cli')` 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ciSet the key to the specified value, at the specified level in the config 2001cb0ef41Sopenharmony_cistack. 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci### `config.delete(key, where = 'cli')` 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ciDelete the configuration key from the specified level in the config stack. 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci### `config.validate(where)` 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ciVerify that all known configuration options are set to valid values, and 2091cb0ef41Sopenharmony_cilog a warning if they are invalid. 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ciInvalid auth options will cause this method to throw an error with a `code` 2121cb0ef41Sopenharmony_ciproperty of `ERR_INVALID_AUTH`, and a `problems` property listing the specific 2131cb0ef41Sopenharmony_ciconcerns with the current configuration. 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ciIf `where` is not set, then all config objects are validated. 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ciReturns `true` if all configs are valid. 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ciNote that it's usually enough (and more efficient) to just check 2201cb0ef41Sopenharmony_ci`config.valid`, since each data object is marked for re-evaluation on every 2211cb0ef41Sopenharmony_ci`config.set()` operation. 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ci### `config.repair(problems)` 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ciAccept an optional array of problems (as thrown by `config.validate()`) and 2261cb0ef41Sopenharmony_ciperform the necessary steps to resolve them. If no problems are provided, 2271cb0ef41Sopenharmony_cithis method will call `config.validate()` internally to retrieve them. 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ciNote that you must `await config.save('user')` in order to persist the changes. 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ci### `config.isDefault(key)` 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ciReturns `true` if the value is coming directly from the 2341cb0ef41Sopenharmony_cidefault definitions, if the current value for the key config is 2351cb0ef41Sopenharmony_cicoming from any other source, returns `false`. 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ciThis method can be used for avoiding or tweaking default values, e.g: 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci> Given a global default definition of foo='foo' it's possible to read that 2401cb0ef41Sopenharmony_ci> value such as: 2411cb0ef41Sopenharmony_ci> 2421cb0ef41Sopenharmony_ci> ```js 2431cb0ef41Sopenharmony_ci> const save = config.get('foo') 2441cb0ef41Sopenharmony_ci> ``` 2451cb0ef41Sopenharmony_ci> 2461cb0ef41Sopenharmony_ci> Now in a different place of your app it's possible to avoid using the `foo` 2471cb0ef41Sopenharmony_ci> default value, by checking to see if the current config value is currently 2481cb0ef41Sopenharmony_ci> one that was defined by the default definitions: 2491cb0ef41Sopenharmony_ci> 2501cb0ef41Sopenharmony_ci> ```js 2511cb0ef41Sopenharmony_ci> const save = config.isDefault('foo') ? 'bar' : config.get('foo') 2521cb0ef41Sopenharmony_ci> ``` 2531cb0ef41Sopenharmony_ci 2541cb0ef41Sopenharmony_ci### `config.save(where)` 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_ciSave the config file specified by the `where` param. Must be one of 2571cb0ef41Sopenharmony_ci`project`, `user`, `global`, `builtin`. 258