Configuration management through .nirc files and environment variables, allowing users to set default and global package managers.
Load and manage ni configuration from files and environment variables.
/**
* Get current ni configuration
* @returns Promise resolving to configuration object
*/
function getConfig(): Promise<Config>;
/**
* Get default package manager agent
* @param programmatic - Running in programmatic mode
* @returns Promise resolving to default agent or 'prompt'
*/
function getDefaultAgent(programmatic?: boolean): Promise<Agent | 'prompt'>;
/**
* Get global package manager agent
* @returns Promise resolving to global agent
*/
function getGlobalAgent(): Promise<Agent>;
interface Config {
/** Default agent for project installs ('prompt' shows selection menu) */
defaultAgent: Agent | 'prompt';
/** Agent for global installs */
globalAgent: Agent;
}Usage Examples:
import { getConfig, getDefaultAgent, getGlobalAgent } from "@antfu/ni";
// Get full configuration
const config = await getConfig();
console.log(config);
// { defaultAgent: 'npm', globalAgent: 'npm' }
// Get specific agents
const defaultAgent = await getDefaultAgent();
console.log(defaultAgent); // 'npm' | 'yarn' | 'pnpm' | 'bun' | 'prompt'
const globalAgent = await getGlobalAgent();
console.log(globalAgent); // 'npm' | 'yarn' | 'pnpm' | 'bun'
// In programmatic mode, 'prompt' becomes 'npm'
const programmaticAgent = await getDefaultAgent(true);
console.log(programmaticAgent); // Never 'prompt' in programmatic modeConfiguration is loaded from multiple sources in order of precedence:
Highest priority configuration source:
# Default agent for project operations
export NI_DEFAULT_AGENT="pnpm"
# Global agent for global installs
export NI_GLOBAL_AGENT="npm"
# Custom config file location
export NI_CONFIG_FILE="$HOME/.config/ni/nirc"
# Auto-install missing package managers
export NI_AUTO_INSTALL="true"Default location: ~/.nirc (or $NI_CONFIG_FILE if set)
Format: INI-style key-value pairs
# ~/.nirc
# Default agent for project installs
# Options: npm, yarn, pnpm, bun, prompt
defaultAgent=pnpm
# Agent for global installs
# Options: npm, yarn, pnpm, bun
globalAgent=npmIf no configuration is found, ni will:
// Default configuration paths
const defaultRcPath = "~/.nirc"; // Unix/Linux/macOS
const defaultRcPath = "%USERPROFILE%\\.nirc"; // Windows
// Custom path via environment variable
const customRcPath = process.env.NI_CONFIG_FILE;The .nirc file uses INI format:
# Fallback when no lock file is found
# Values: npm, yarn, pnpm, bun, prompt
defaultAgent=prompt
# For global installs (ni -g, nun -g)
# Values: npm, yarn, pnpm, bun
globalAgent=npm# Create basic configuration
echo "defaultAgent=pnpm" > ~/.nirc
echo "globalAgent=npm" >> ~/.nirc
# Or use custom location
mkdir -p ~/.config/ni
echo "defaultAgent=yarn" > ~/.config/ni/nirc
export NI_CONFIG_FILE="$HOME/.config/ni/nirc"All configuration can be controlled via environment variables:
interface EnvironmentVariables {
/** Custom config file path */
NI_CONFIG_FILE?: string;
/** Default agent (overrides config file) */
NI_DEFAULT_AGENT?: Agent;
/** Global agent (overrides config file) */
NI_GLOBAL_AGENT?: Agent;
/** Auto-install missing package managers */
NI_AUTO_INSTALL?: 'true' | 'false';
/** CI environment flag (affects prompts) */
CI?: string;
}Bash/Zsh (.bashrc, .zshrc):
# Set default package manager
export NI_DEFAULT_AGENT="pnpm"
export NI_GLOBAL_AGENT="npm"
# Enable auto-installation
export NI_AUTO_INSTALL="true"
# Custom config location
export NI_CONFIG_FILE="$HOME/.config/ni/nirc"PowerShell (Microsoft.PowerShell_profile.ps1):
# Set default package manager
$Env:NI_DEFAULT_AGENT = "pnpm"
$Env:NI_GLOBAL_AGENT = "npm"
# Custom config location
$Env:NI_CONFIG_FILE = "C:\Users\$Env:USERNAME\.config\ni\nirc"Windows CMD:
set NI_DEFAULT_AGENT=pnpm
set NI_GLOBAL_AGENT=npm
set NI_CONFIG_FILE=%USERPROFILE%\.config\ni\nircNI_DEFAULT_AGENT if setdefaultAgent from .nircNI_GLOBAL_AGENT if setglobalAgent from .nircWhen defaultAgent is set to 'prompt':
// Interactive selection in non-CI environments
const agents = ['npm', 'yarn', 'pnpm', 'bun'];
// User selects from menu
// Automatic fallback in CI or programmatic mode
const agent = 'npm'; // Default fallbackni automatically detects project package managers:
// Detection priority order:
// 1. packageManager field in package.json
// 2. Lock file presence:
// - package-lock.json → npm
// - yarn.lock → yarn
// - pnpm-lock.yaml → pnpm
// - bun.lock/bun.lockb → bun
// 3. Configuration fallbackWhen Volta is detected, commands are automatically wrapped:
interface VoltaIntegration {
/** Enable Volta detection (default: true) */
detectVolta?: boolean;
}
// Example: ni install becomes volta run npm installSpecial behavior in CI environments:
// When process.env.CI is set:
// - No interactive prompts
// - Auto-install prompts become errors
// - 'prompt' defaults to 'npm'
// - Reduced console output# ~/.nirc for consistent team setup
defaultAgent=pnpm
globalAgent=npm# .bashrc for team consistency
export NI_DEFAULT_AGENT="pnpm"
export NI_GLOBAL_AGENT="npm"
export NI_AUTO_INSTALL="true"# Different agents per project type
alias ni-npm='NI_DEFAULT_AGENT=npm ni'
alias ni-pnpm='NI_DEFAULT_AGENT=pnpm ni'
alias ni-yarn='NI_DEFAULT_AGENT=yarn ni'# GitHub Actions example
env:
NI_DEFAULT_AGENT: "npm"
NI_AUTO_INSTALL: "false"
CI: "true"Windows PowerShell has a built-in ni alias. To resolve:
# Remove PowerShell's ni alias
Remove-Item Alias:ni -Force -ErrorAction Ignore
# Add to profile for persistence
if (-not (Test-Path $profile)) {
New-Item -ItemType File -Path (Split-Path $profile) -Force -Name (Split-Path $profile -Leaf)
}
$profileEntry = 'Remove-Item Alias:ni -Force -ErrorAction Ignore'
$profileContent = Get-Content $profile
if ($profileContent -notcontains $profileEntry) {
("`n" + $profileEntry) | Out-File $profile -Append -Force -Encoding UTF8
}