README.md
1# `@npmcli/config`
2
3Configuration management for the npm cli.
4
5This module is the spiritual descendant of
6[`npmconf`](http://npm.im/npmconf), and the code that once lived in npm's
7`lib/config/` folder.
8
9It does the management of configuration files that npm uses, but
10importantly, does _not_ define all the configuration defaults or types, as
11those parts make more sense to live within the npm CLI itself.
12
13The only exceptions:
14
15- The `prefix` config value has some special semantics, setting the local
16 prefix if specified on the CLI options and not in global mode, or the
17 global prefix otherwise.
18- The `project` config file is loaded based on the local prefix (which can
19 only be set by the CLI config options, and otherwise defaults to a walk
20 up the folder tree to the first parent containing a `node_modules`
21 folder, `package.json` file, or `package-lock.json` file.)
22- The `userconfig` value, as set by the environment and CLI (defaulting to
23 `~/.npmrc`, is used to load user configs.
24- The `globalconfig` value, as set by the environment, CLI, and
25 `userconfig` file (defaulting to `$PREFIX/etc/npmrc`) is used to load
26 global configs.
27- A `builtin` config, read from a `npmrc` file in the root of the npm
28 project itself, overrides all defaults.
29
30The resulting hierarchy of configs:
31
32- CLI switches. eg `--some-key=some-value` on the command line. These are
33 parsed by [`nopt`](http://npm.im/nopt), which is not a great choice, but
34 it's the one that npm has used forever, and changing it will be
35 difficult.
36- Environment variables. eg `npm_config_some_key=some_value` in the
37 environment. There is no way at this time to modify this prefix.
38- INI-formatted project configs. eg `some-key = some-value` in the
39 `localPrefix` folder (ie, the `cwd`, or its nearest parent that contains
40 either a `node_modules` folder or `package.json` file.)
41- INI-formatted userconfig file. eg `some-key = some-value` in `~/.npmrc`.
42 The `userconfig` config value can be overridden by the `cli`, `env`, or
43 `project` configs to change this value.
44- INI-formatted globalconfig file. eg `some-key = some-value` in
45 the `globalPrefix` folder, which is inferred by looking at the location
46 of the node executable, or the `prefix` setting in the `cli`, `env`,
47 `project`, or `userconfig`. The `globalconfig` value at any of those
48 levels can override this.
49- INI-formatted builtin config file. eg `some-key = some-value` in
50 `/usr/local/lib/node_modules/npm/npmrc`. This is not configurable, and
51 is determined by looking in the `npmPath` folder.
52- Default values (passed in by npm when it loads this module).
53
54## USAGE
55
56```js
57const Config = require('@npmcli/config')
58// the types of all the configs we know about
59const types = require('./config/types.js')
60// default values for all the configs we know about
61const defaults = require('./config/defaults.js')
62// if you want -c to be short for --call and so on, define it here
63const shorthands = require('./config/shorthands.js')
64
65const conf = new Config({
66 // path to the npm module being run
67 npmPath: resolve(__dirname, '..'),
68 types,
69 shorthands,
70 defaults,
71 // optional, defaults to process.argv
72 argv: process.argv,
73 // optional, defaults to process.env
74 env: process.env,
75 // optional, defaults to process.execPath
76 execPath: process.execPath,
77 // optional, defaults to process.platform
78 platform: process.platform,
79 // optional, defaults to process.cwd()
80 cwd: process.cwd(),
81})
82
83// emits log events on the process object
84// see `proc-log` for more info
85process.on('log', (level, ...args) => {
86 console.log(level, ...args)
87})
88
89// returns a promise that fails if config loading fails, and
90// resolves when the config object is ready for action
91conf.load().then(() => {
92 conf.validate()
93 console.log('loaded ok! some-key = ' + conf.get('some-key'))
94}).catch(er => {
95 console.error('error loading configs!', er)
96})
97```
98
99## API
100
101The `Config` class is the sole export.
102
103```js
104const Config = require('@npmcli/config')
105```
106
107### static `Config.typeDefs`
108
109The type definitions passed to `nopt` for CLI option parsing and known
110configuration validation.
111
112### constructor `new Config(options)`
113
114Options:
115
116- `types` Types of all known config values. Note that some are effectively
117 given semantic value in the config loading process itself.
118- `shorthands` An object mapping a shorthand value to an array of CLI
119 arguments that replace it.
120- `defaults` Default values for each of the known configuration keys.
121 These should be defined for all configs given a type, and must be valid.
122- `npmPath` The path to the `npm` module, for loading the `builtin` config
123 file.
124- `cwd` Optional, defaults to `process.cwd()`, used for inferring the
125 `localPrefix` and loading the `project` config.
126- `platform` Optional, defaults to `process.platform`. Used when inferring
127 the `globalPrefix` from the `execPath`, since this is done diferently on
128 Windows.
129- `execPath` Optional, defaults to `process.execPath`. Used to infer the
130 `globalPrefix`.
131- `env` Optional, defaults to `process.env`. Source of the environment
132 variables for configuration.
133- `argv` Optional, defaults to `process.argv`. Source of the CLI options
134 used for configuration.
135
136Returns a `config` object, which is not yet loaded.
137
138Fields:
139
140- `config.globalPrefix` The prefix for `global` operations. Set by the
141 `prefix` config value, or defaults based on the location of the
142 `execPath` option.
143- `config.localPrefix` The prefix for `local` operations. Set by the
144 `prefix` config value on the CLI only, or defaults to either the `cwd` or
145 its nearest ancestor containing a `node_modules` folder or `package.json`
146 file.
147- `config.sources` A read-only `Map` of the file (or a comment, if no file
148 found, or relevant) to the config level loaded from that source.
149- `config.data` A `Map` of config level to `ConfigData` objects. These
150 objects should not be modified directly under any circumstances.
151 - `source` The source where this data was loaded from.
152 - `raw` The raw data used to generate this config data, as it was parsed
153 initially from the environment, config file, or CLI options.
154 - `data` The data object reflecting the inheritance of configs up to this
155 point in the chain.
156 - `loadError` Any errors encountered that prevented the loading of this
157 config data.
158- `config.list` A list sorted in priority of all the config data objects in
159 the prototype chain. `config.list[0]` is the `cli` level,
160 `config.list[1]` is the `env` level, and so on.
161- `cwd` The `cwd` param
162- `env` The `env` param
163- `argv` The `argv` param
164- `execPath` The `execPath` param
165- `platform` The `platform` param
166- `defaults` The `defaults` param
167- `shorthands` The `shorthands` param
168- `types` The `types` param
169- `npmPath` The `npmPath` param
170- `globalPrefix` The effective `globalPrefix`
171- `localPrefix` The effective `localPrefix`
172- `prefix` If `config.get('global')` is true, then `globalPrefix`,
173 otherwise `localPrefix`
174- `home` The user's home directory, found by looking at `env.HOME` or
175 calling `os.homedir()`.
176- `loaded` A boolean indicating whether or not configs are loaded
177- `valid` A getter that returns `true` if all the config objects are valid.
178 Any data objects that have been modified with `config.set(...)` will be
179 re-evaluated when `config.valid` is read.
180
181### `config.load()`
182
183Load configuration from the various sources of information.
184
185Returns a `Promise` that resolves when configuration is loaded, and fails
186if a fatal error is encountered.
187
188### `config.find(key)`
189
190Find the effective place in the configuration levels a given key is set.
191Returns one of: `cli`, `env`, `project`, `user`, `global`, `builtin`, or
192`default`.
193
194Returns `null` if the key is not set.
195
196### `config.get(key, where = 'cli')`
197
198Load the given key from the config stack.
199
200### `config.set(key, value, where = 'cli')`
201
202Set the key to the specified value, at the specified level in the config
203stack.
204
205### `config.delete(key, where = 'cli')`
206
207Delete the configuration key from the specified level in the config stack.
208
209### `config.validate(where)`
210
211Verify that all known configuration options are set to valid values, and
212log a warning if they are invalid.
213
214Invalid auth options will cause this method to throw an error with a `code`
215property of `ERR_INVALID_AUTH`, and a `problems` property listing the specific
216concerns with the current configuration.
217
218If `where` is not set, then all config objects are validated.
219
220Returns `true` if all configs are valid.
221
222Note that it's usually enough (and more efficient) to just check
223`config.valid`, since each data object is marked for re-evaluation on every
224`config.set()` operation.
225
226### `config.repair(problems)`
227
228Accept an optional array of problems (as thrown by `config.validate()`) and
229perform the necessary steps to resolve them. If no problems are provided,
230this method will call `config.validate()` internally to retrieve them.
231
232Note that you must `await config.save('user')` in order to persist the changes.
233
234### `config.isDefault(key)`
235
236Returns `true` if the value is coming directly from the
237default definitions, if the current value for the key config is
238coming from any other source, returns `false`.
239
240This method can be used for avoiding or tweaking default values, e.g:
241
242> Given a global default definition of foo='foo' it's possible to read that
243> value such as:
244>
245> ```js
246> const save = config.get('foo')
247> ```
248>
249> Now in a different place of your app it's possible to avoid using the `foo`
250> default value, by checking to see if the current config value is currently
251> one that was defined by the default definitions:
252>
253> ```js
254> const save = config.isDefault('foo') ? 'bar' : config.get('foo')
255> ```
256
257### `config.save(where)`
258
259Save the config file specified by the `where` param. Must be one of
260`project`, `user`, `global`, `builtin`.
261