registry-auth-token
raw JSON → 5.1.1 verified Sat Apr 25 auth: no javascript
Retrieves npm registry authentication tokens from .npmrc or programmatic configuration. Current stable version 5.1.1 (Node >=14). Provides both token and type (Bearer/Basic). Also offers registry URL resolution for scopes. Lightweight, no dependencies, ships TypeScript definitions. Critical for tools that need to authenticate with npm registries programmatically.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module not supported ↓
cause Using require() to load the ESM-only package v5
fix
Use dynamic import: const { getAuthToken } = await import('registry-auth-token') or switch to ESM
error SyntaxError: The requested module 'registry-auth-token' does not provide an export named 'default' ↓
cause Using default import instead of named import in v5
fix
Change import to: import { getAuthToken } from 'registry-auth-token'
error getRegistryUrl is not a function ↓
cause Importing getRegistryUrl from main entry (not available there in v5)
fix
Import from 'registry-auth-token/registry-url': import { getRegistryUrl } from 'registry-auth-token/registry-url'
Warnings
breaking Default export removed in v5 - must use named import { getAuthToken } instead of import getAuthToken from '...' ↓
fix Change to import { getAuthToken } from 'registry-auth-token'
breaking Subpath export getRegistryUrl moved to 'registry-auth-token/registry-url' - no longer exposed from main entry ↓
fix Use import { getRegistryUrl } from 'registry-auth-token/registry-url'
breaking Drop support for Node <14 - may fail on older Node versions ↓
fix Upgrade Node.js to version 14 or later
gotcha Token can be undefined - code must handle missing auth gracefully ↓
fix Check if token is truthy before accessing token.token or token.type
Install
npm install registry-auth-token yarn add registry-auth-token pnpm add registry-auth-token Imports
- getAuthToken wrong
import getAuthToken from 'registry-auth-token'correctimport { getAuthToken } from 'registry-auth-token' - getRegistryUrl wrong
import { getRegistryUrl } from 'registry-auth-token'correctimport { getRegistryUrl } from 'registry-auth-token/registry-url' - NpmConfig
import type { NpmConfig } from 'registry-auth-token'
Quickstart
import { getAuthToken, getRegistryUrl } from 'registry-auth-token';
const token = getAuthToken();
if (token) {
console.log('Token:', token.token);
console.log('Type:', token.type);
} else {
console.log('No token found');
}
const registryUrl = getRegistryUrl('@scope');
console.log('Registry URL:', registryUrl);