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(®istry) {
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):
- Project -
{localPrefix}/.npmrc(found by walking up from cwd) - User -
~/.npmrc - 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-passwordModules§
- registry
- Registry URL resolution for npm packages.
Structs§
- Client
Cert - Client certificate for mTLS authentication.
- Config
Data - Parsed configuration data from a single .npmrc file.
- Load
Options - Options for loading npm configuration.
- Npmrc
Config - 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.jsonfile or anode_modulesdirectory. - 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.