Detalhes do pacote

@npmcli/config

npm4.7mISC10.2.0

Configuration management for the npm cli

readme (leia-me)

@npmcli/config

Configuration management for the npm cli.

This module is the spiritual descendant 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 the npm CLI 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 hierarchy of configs:

  • CLI switches. eg --some-key=some-value on the command line. These are parsed by nopt, which is not a great choice, but it's the one that npm has used forever, and changing it will be difficult.
  • Environment variables. eg npm_config_some_key=some_value in the environment. There is no way at this time to modify this prefix.
  • INI-formatted project configs. eg some-key = some-value in the localPrefix folder (ie, the cwd, or its nearest parent that contains either a node_modules folder or package.json file.)
  • INI-formatted userconfig file. eg some-key = some-value in ~/.npmrc. The userconfig config value can be overridden by the cli, env, or project configs to change this value.
  • INI-formatted globalconfig file. eg some-key = some-value in the globalPrefix folder, which is inferred by looking at the location of the node executable, or the prefix setting in the cli, env, project, or userconfig. The globalconfig value at any of those levels can override this.
  • INI-formatted builtin config file. eg some-key = some-value in /usr/local/lib/node_modules/npm/npmrc. This is not configurable, and is determined by looking in the npmPath folder.
  • Default values (passed in by npm when it loads this module).

USAGE

const Config = require('@npmcli/config')
const { shorthands, definitions, flatten } = require('@npmcli/config/lib/definitions')

const conf = new Config({
  // path to the npm module being run
  npmPath: resolve(__dirname, '..'),
  definitions,
  shorthands,
  flatten,
  // optional, defaults to process.argv
  // argv: [] <- if you are using this package in your own cli
  //             and dont want to have colliding argv
  argv: process.argv,
  // optional, defaults to process.env
  env: process.env,
  // optional, defaults to process.execPath
  execPath: process.execPath,
  // optional, defaults to process.platform
  platform: process.platform,
  // optional, defaults to process.cwd()
  cwd: process.cwd(),
})

// emits log events on the process object
// see `proc-log` for more info
process.on('log', (level, ...args) => {
  console.log(level, ...args)
})

// returns a promise that fails if config loading fails, and
// resolves when the config object is ready for action
conf.load().then(() => {
  conf.validate()
  console.log('loaded ok! some-key = ' + conf.get('some-key'))
}).catch(er => {
  console.error('error loading configs!', er)
})

API

The Config class is the sole export.

const Config = require('@npmcli/config')

static Config.typeDefs

The type definitions passed to nopt for CLI option parsing and known configuration validation.

constructor new Config(options)

Options:

  • types Types of all known config values. Note that some are effectively given semantic value in the config loading process itself.
  • shorthands An object mapping a shorthand value to an array of CLI arguments that replace it.
  • defaults Default values for each of the known configuration keys. These should be defined for all configs given a type, and must be valid.
  • npmPath The path to the npm module, for loading the builtin config file.
  • cwd Optional, defaults to process.cwd(), used for inferring the localPrefix and loading the project config.
  • platform Optional, defaults to process.platform. Used when inferring the globalPrefix from the execPath, since this is done diferently on Windows.
  • execPath Optional, defaults to process.execPath. Used to infer the globalPrefix.
  • env Optional, defaults to process.env. Source of the environment variables for configuration.
  • argv Optional, defaults to process.argv. Source of the CLI options used for configuration.

Returns a config object, which is not yet loaded.

Fields:

  • config.globalPrefix The prefix for global operations. Set by the prefix config value, or defaults based on the location of the execPath option.
  • config.localPrefix The prefix for local operations. Set by the prefix config value on the CLI only, or defaults to either the cwd or its nearest ancestor containing a node_modules folder or package.json file.
  • config.sources A read-only Map of the file (or a comment, if no file found, or relevant) to the config level loaded from that source.
  • config.data A Map of config level to ConfigData objects. These objects should not be modified directly under any circumstances.
    • source The source where this data was loaded from.
    • raw The raw data used to generate this config data, as it was parsed initially from the environment, config file, or CLI options.
    • data The data object reflecting the inheritance of configs up to this point in the chain.
    • loadError Any errors encountered that prevented the loading of this config data.
  • config.list A list sorted in priority of all the config data objects in the prototype chain. config.list[0] is the cli level, config.list[1] is the env level, and so on.
  • cwd The cwd param
  • env The env param
  • argv The argv param
  • execPath The execPath param
  • platform The platform param
  • defaults The defaults param
  • shorthands The shorthands param
  • types The types param
  • npmPath The npmPath param
  • globalPrefix The effective globalPrefix
  • localPrefix The effective localPrefix
  • prefix If config.get('global') is true, then globalPrefix, otherwise localPrefix
  • home The user's home directory, found by looking at env.HOME or calling os.homedir().
  • loaded A boolean indicating whether or not configs are loaded
  • valid A getter that returns true if all the config objects are valid. Any data objects that have been modified with config.set(...) will be re-evaluated when config.valid is read.

config.load()

Load configuration from the various sources of information.

Returns a Promise that resolves when configuration is loaded, and fails if a fatal error is encountered.

config.find(key)

Find the effective place in the configuration levels a given key is set. Returns one of: cli, env, project, user, global, builtin, or default.

Returns null if the key is not set.

config.get(key, where = 'cli')

Load the given key from the config stack.

config.set(key, value, where = 'cli')

Set the key to the specified value, at the specified level in the config stack.

config.delete(key, where = 'cli')

Delete the configuration key from the specified level in the config stack.

config.validate(where)

Verify that all known configuration options are set to valid values, and log a warning if they are invalid.

Invalid auth options will cause this method to throw an error with a code property of ERR_INVALID_AUTH, and a problems property listing the specific concerns with the current configuration.

If where is not set, then all config objects are validated.

Returns true if all configs are valid.

Note that it's usually enough (and more efficient) to just check config.valid, since each data object is marked for re-evaluation on every config.set() operation.

config.repair(problems)

Accept an optional array of problems (as thrown by config.validate()) and perform the necessary steps to resolve them. If no problems are provided, this method will call config.validate() internally to retrieve them.

Note that you must await config.save('user') in order to persist the changes.

config.isDefault(key)

Returns true if the value is coming directly from the default definitions, if the current value for the key config is coming from any other source, returns false.

This method can be used for avoiding or tweaking default values, e.g:

Given a global default definition of foo='foo' it's possible to read that value such as:

    const save = config.get('foo')

Now in a different place of your app it's possible to avoid using the foo default value, by checking to see if the current config value is currently one that was defined by the default definitions:

    const save = config.isDefault('foo') ? 'bar' : config.get('foo')

config.save(where)

Save the config file specified by the where param. Must be one of project, user, global, builtin.

changelog (log de mudanças)

Changelog

11.3.0 (2025-04-08)

Features

11.2.0 (2025-03-05)

Features

11.1.0 (2025-01-29)

Features

  • 7f6c997 #8009 add dry-run to deprecate/undeprecate commands (@wraithgar)
  • 1764a37 #8009 add npm undeprecate command (@wraithgar)

    Bug Fixes

  • 31455b2 #8054 publish: honor force for no dist tag and registry version check (#8054) (@reggi)
  • dc31c1b #8038 remove max-len linting bypasses (@wraithgar)
  • 8a911ff #8038 publish: disregard deprecated versions when calculating highest version (@wraithgar)
  • 7f72944 #8038 publish: accept publishConfig.tag to override highes semver check (@wraithgar)
  • ab9ddc0 #7992 sbom: deduplicate sbom dependencies (#7992) (@bdehamer)
  • f7da341 #7980 search: properly display multiple search terms (#7980) (@wraithgar)

    Documentation

  • 3644e79 #8055 update readme for Node.js versions, remove badges (#8055) (@wraithgar)
  • f1af61f #8041 fix typos in "package-json" (#8041) (@maxkoryukov)
  • e90c6fe #8051 depth flag default value (#8051) (@milaninfy)
  • 866b5ee #8030 safer documentation urls, repos, packages (#8030) (@reggi)

    Dependencies

  • 7ddfbad #8053 @npmcli/package-json@6.1.1
  • 9473a86 #8053 spdx-license-ids@3.0.21
  • a65e5ce #8053 @sigstore/protobuf-specs@0.3.3
  • 215ebe4 #8053 chalk@5.4.1

    Chores

  • 61f00e3 #8069 splits out smoke-tests from publish-dryrun tests (#8069) (@reggi)
  • 6d0f46e #8058 stop publish smoke from check git clean (#8058) (@reggi)
  • 9281ebf #8057 fix smoke tests prerelease needs separate string args (#8057) (@reggi)
  • aa202e9 #8056 smoke tests using a preid (#8056) (@reggi)
  • 18e0449 #8053 dev dependency updates (@wraithgar)
  • 859a71c #8052 update node versions for release integration tests (#8052) (@wraithgar)
  • 7e7961d #8038 bump @npmcli/eslint-config to 5.1.0 (@wraithgar)
  • workspace: @npmcli/config@10.0.1

11.0.0 (2024-12-16)

Documentation

11.0.0-pre.1 (2024-12-06)

⚠️ BREAKING CHANGES

  • Upon publishing, in order to apply a default "latest" dist tag, the command now retrieves all prior versions of the package. It will require that the version you're trying to publish is above the latest semver version in the registry, not including pre-release tags.
  • npm init now has a type prompt, and sorts the entries the created packages differently
  • bun.lockb files are now included in the strict ignore list during packing

    Features

  • f3ac7b7 #7939 no implicit latest tag on publish when latest > version (#7939) (@reggi, @ljharb)

    Bug Fixes

  • e362c6d #7944 prefix: remove duplicate -g from usage output (#7944) (@wraithgar)

    Documentation

  • 2af31dd #7947 change certfile to cafile (#7947) (@wraithgar)
  • 1be8e95 #7945 update ignore rules (@wraithgar)

    Dependencies

  • bc9b14d #7955 @npmcli/run-script@9.0.2
  • fecfcf4 #7955 node-gyp@11.0.0
  • 8905037 #7955 p-map@7.0.2
  • ac8eb39 #7955 diff@7.0.0
  • c0bcc2a #7955 walk-up-path@4.0.0
  • d463a6f #7955 init-package-json@8.0.0
  • b87ba24 #7945 @npmcli/package-json@6.1.0
  • 4bf1901 #7945 @npmcli/metavuln-calculator@9.0.0
  • ca84b22 #7945 pacote@21.0.0
  • 4906f3d #7945 npm-packlist@10.0.0

    Chores

  • cfdf214 #7943 fork changelog (#7943) (@wraithgar)
  • workspace: @npmcli/arborist@9.0.0-pre.1
  • workspace: @npmcli/config@10.0.0-pre.1
  • workspace: libnpmdiff@8.0.0-pre.1
  • workspace: libnpmexec@10.0.0-pre.1
  • workspace: libnpmfund@7.0.0-pre.1
  • workspace: libnpmorg@8.0.0-pre.1
  • workspace: libnpmpack@9.0.0-pre.1

11.0.0-pre.0 (2024-11-26)

⚠️ BREAKING CHANGES

  • When publishing a package with a pre-release version, you must explicitly specify a tag.
  • --ignore-scripts now applies to all lifecycle scripts, include prepare
  • npm will no longer fall back to the old audit endpoint if the bulk advisory request fails.
  • npm will no longer switch to global mode if aliased to "npmg" or "npm-g" etc.
  • The npm hook command has been removed
  • Attestations made by this package will no longer validate in npm versions prior to 10.6.0
  • npm now supports node ^20.17.0 || >=22.9.0
  • @npmcli/docs now supports node ^20.17.0 || >=22.9.0

    Features

  • 6995303 #7850 adds --ignore-scripts flag to pack (@reggi)

    Bug Fixes

  • 16b7367 #7910 publishing prerelease requires explicit tag (#7910) (@reggi)
  • e19bff0 #7901 perf: enable compile cache if present (#7901) (@H4ad)
  • 080a0f2 #7911 remove old audit fallback request (@wraithgar)
  • 780afc5 #7855 pkg: display if any of multiple attributes exist (#7855) (@Sanderovich)
  • ecd2d23 #7842 don't go into global mode if aliased to npmg (#7842) (@wraithgar)
  • 62c71e5 #7835 removes npm hook command (@reggi)
  • 7f541e8 #7815 make pack and exec work with git hash refs (#7815) (@milaninfy)
  • 3162620 #7831 sets node engine range to ^20.17.0 || >=22.9.0 (@reggi)
  • 4c8ba0a #7831 for @npmcli/docs sets node engine range to ^20.17.0 || >=22.9.0 (@reggi)
  • 70cd88d #7808 view: sort and truncate dist-tags (#7808) (@wraithgar)
  • 534ad77 #7795 remove unused parameters catch statements (#7795) (@btea)

    Documentation

  • feb54f7 #7822 package.json: add libc field (#7822) (@wraithgar)

    Dependencies

  • 78293ad #7937 spdx-license-ids@3.0.20
  • 33cf580 #7937 promise-call-limit@3.0.2
  • ef1c368 #7937 package-json-from-dist@1.0.1
  • 92e6f07 #7937 npm-registry-fetch@18.0.2
  • e32284a #7937 npm-install-checks@7.1.1
  • 5dffd11 #7937 negotiator@0.6.4
  • 69d9f01 #7937 make-fetch-happen@14.0.3
  • 884bbde #7937 hosted-git-info@8.0.2
  • 3c74ec0 #7937 debug@4.3.7
  • f00359f #7937 cross-spawn@7.0.6
  • 534bbe8 #7937 ci-info@4.1.0
  • 8cbf1a7 #7937 @npmcli/promise-spawn@8.0.2
  • 1bd39e7 #7937 @npmcli/map-workspaces@4.0.2
  • eb6498d #7937 ansi-regex@6.1.0
  • 66fc8c9 #7850 @npmcli/metavuln-calculator@8.0.1
  • 7dbef6f #7850 pacote@20.0.0
  • 75a3f12 #7859 remove unused deps (#7859)
  • f36dc59 #7833 pacote@19.0.1
  • 7ee15bb #7833 bump sigstore from 2.x to 3.0.0 (@bdehamer)

    Chores

  • 2d530a5 #7941 tests: account for when npm is a prerelease (#7941) (@wraithgar)
  • 2c1b369 #7937 dev dependency updates (@wraithgar)
  • 6edfe2f #7937 @npmcli/template-oss@4.23.5 (@wraithgar)
  • 475285b #7920 clean up dependency graph repos (#7920) (@hashtagchris)
  • ec57f5f #7911 fix dependencies script for circular workspace deps (@wraithgar)
  • ccd8420 #7911 fix cli tests for audit fallback removal (@wraithgar)
  • 720b4d8 #7833 bump @npmcli/arborist to 8.0.0 (@wraithgar)
  • 286739c #7824 add creation of a DEPENDENCIES.json file (#7824) (@reggi)
  • 852dd8b #7831 sets npm 11 to prerelase (@reggi)
  • 95d009e #7831 update engine ^20.17.0 || >=22.9.0 in actions (@reggi)
  • 5a74478 #7831 update engines ^20.17.0 || >=22.9.0 in package template (@reggi)
  • workspace: @npmcli/arborist@9.0.0-pre.0
  • workspace: @npmcli/config@10.0.0-pre.0
  • workspace: libnpmaccess@10.0.0-pre.0
  • workspace: libnpmdiff@8.0.0-pre.0
  • workspace: libnpmexec@10.0.0-pre.0
  • workspace: libnpmfund@7.0.0-pre.0
  • workspace: libnpmorg@8.0.0-pre.0
  • workspace: libnpmpack@9.0.0-pre.0
  • workspace: libnpmpublish@11.0.0-pre.0
  • workspace: libnpmsearch@9.0.0-pre.0
  • workspace: libnpmteam@8.0.0-pre.0
  • workspace: libnpmversion@8.0.0-pre.0