Skip to main content

Crate npmrc_config_rs

Crate npmrc_config_rs 

Source
Expand description

Rust port of npm .npmrc configuration logic.

This crate provides functionality to load and query npm configuration from .npmrc files, including support for:

  • Multiple config levels (global, user, project)
  • Scoped registries
  • Authentication (bearer tokens, basic auth, mTLS)
  • Environment variable expansion

§Quick Start

use npmrc_config_rs::NpmrcConfig;

// Load config from standard locations
let config = NpmrcConfig::load().unwrap();

// Get registry for a package
let registry = config.registry_for("@myorg/package");

// Get credentials for authentication
if let Some(creds) = config.credentials_for(&registry) {
    match creds {
        npmrc_config_rs::Credentials::Token { token, .. } => {
            println!("Using token: {}", token);
        }
        npmrc_config_rs::Credentials::BasicAuth { username, password, .. } => {
            println!("Using basic auth: {}:***", username);
        }
        _ => {}
    }
}

§Configuration Priority

Configuration is loaded from multiple levels with the following priority (highest to lowest):

  1. Project - {localPrefix}/.npmrc (found by walking up from cwd)
  2. User - ~/.npmrc
  3. Global - {globalPrefix}/etc/npmrc

Values from higher-priority sources override lower-priority ones.

§Authentication (Nerf-Darting)

npm uses “nerf-darting” to scope credentials to specific registries, preventing credential leakage. Registry URLs are converted to a canonical form:

https://registry.npmjs.org/ → //registry.npmjs.org/

Auth configuration uses this format:

//registry.npmjs.org/:_authToken = your-token
//private.registry.com/:username = user
//private.registry.com/:_password = base64-encoded-password

Modules§

registry
Registry URL resolution for npm packages.

Structs§

ClientCert
Client certificate for mTLS authentication.
ConfigData
Parsed configuration data from a single .npmrc file.
LoadOptions
Options for loading npm configuration.
NpmrcConfig
npm configuration loaded from .npmrc files.

Enums§

Credentials
Credentials for authenticating with an npm registry.
Error
Errors that can occur when working with npmrc configuration.

Functions§

expand_env_vars
Expand ${VAR} environment variable references in a value.
expand_tilde
Expand ~ at the start of a path to the user’s home directory.
find_global_prefix
Find the global prefix by locating the node executable and deriving the prefix from its location.
find_local_prefix
Walk up from the given directory looking for the first directory containing either a package.json file or a node_modules directory.
global_config_path
Get the path to the global .npmrc file ({globalPrefix}/etc/npmrc).
nerf_dart
Convert a registry URL to nerf-dart format for credential lookup.
parse_bool
Parse a boolean value from a string.
project_config_path
Get the path to the project .npmrc file ({localPrefix}/.npmrc).
user_config_path
Get the path to the user’s .npmrc file (~/.npmrc).

Type Aliases§

Result
Result type alias for npmrc-config-rs operations.