0
# registry-url
1
2
Get the set npm registry URL. This utility retrieves the currently configured npm registry URL from environment variables and configuration files, supporting both global registry configuration and scoped package registries.
3
4
## Package Information
5
6
- **Package Name**: registry-url
7
- **Package Type**: npm
8
- **Language**: JavaScript (ES modules)
9
- **Installation**: `npm install registry-url`
10
11
## Core Imports
12
13
```javascript
14
import registryUrl, { defaultUrl } from 'registry-url';
15
```
16
17
For CommonJS environments:
18
19
```javascript
20
const registryUrl = require('registry-url').default;
21
const { defaultUrl } = require('registry-url');
22
```
23
24
## Basic Usage
25
26
```javascript
27
import registryUrl, { defaultUrl } from 'registry-url';
28
29
// Get the configured registry URL
30
console.log(registryUrl());
31
//=> 'https://registry.npmjs.org/' (or your configured registry)
32
33
// Get the default npm registry URL
34
console.log(defaultUrl);
35
//=> 'https://registry.npmjs.org/'
36
37
// Get registry URL for a specific scope
38
console.log(registryUrl('@mycompany'));
39
//=> URL configured for @mycompany scope or fallback
40
```
41
42
## Capabilities
43
44
### Registry URL Resolution
45
46
Retrieves the npm registry URL based on configuration priority: environment variables, .npmrc files, and fallback to default.
47
48
```javascript { .api }
49
/**
50
* Get the npm registry URL for the given scope or global registry
51
* @param scope - Optional npm scope (e.g., '@mycompany')
52
* @returns Registry URL, always ending with trailing slash
53
*/
54
function registryUrl(scope?: string): string;
55
```
56
57
**Configuration Priority** (highest to lowest):
58
1. `npm_config_registry` environment variable
59
2. `NPM_CONFIG_REGISTRY` environment variable
60
3. Scoped registry from `.npmrc`: `@scope:registry=url`
61
4. General registry from `.npmrc`: `registry=url`
62
5. Default npm registry
63
64
**Usage Examples:**
65
66
```javascript
67
// Global registry (checks env vars, then .npmrc, then default)
68
const globalRegistry = registryUrl();
69
70
// Scoped registry (checks scope config, then falls back to global logic)
71
const scopedRegistry = registryUrl('@mycompany');
72
```
73
74
**Configuration Examples:**
75
76
Environment variable configuration:
77
```bash
78
export npm_config_registry="https://my-registry.com/"
79
```
80
81
.npmrc file configuration:
82
```ini
83
# Global registry
84
registry=https://my-registry.com/
85
86
# Scoped registry
87
@mycompany:registry=https://company-registry.com/
88
```
89
90
### Default Registry URL
91
92
The standard npm registry URL constant.
93
94
```javascript { .api }
95
/**
96
* The default npm registry URL
97
*/
98
const defaultUrl: string;
99
```
100
101
**Value**: `"https://registry.npmjs.org/"`
102
103
**Usage Examples:**
104
105
```javascript
106
import { defaultUrl } from 'registry-url';
107
108
// Use as fallback or for comparison
109
if (currentRegistry === defaultUrl) {
110
console.log('Using default npm registry');
111
}
112
113
// Use in configuration
114
const config = {
115
registry: process.env.CUSTOM_REGISTRY || defaultUrl
116
};
117
```
118
119
## URL Normalization
120
121
All returned URLs are automatically normalized to include a trailing slash, ensuring consistency for registry API calls.
122
123
```javascript
124
// Input: 'https://registry.example.com'
125
// Output: 'https://registry.example.com/'
126
```
127
128
## Error Handling
129
130
The package gracefully handles common configuration issues:
131
132
- **Missing .npmrc files**: Falls back to environment variables or default
133
- **Invalid .npmrc content**: Uses what can be parsed, falls back gracefully
134
- **Missing scope configuration**: Falls back to general registry configuration
135
- **Missing environment variables**: Uses .npmrc or default registry
136
137
No exceptions are thrown - the function always returns a valid registry URL.
138
139
## Platform Requirements
140
141
- **Node.js**: >= 18
142
- **Module System**: ES modules (with CommonJS compatibility via exports field)
143
- **Platform**: Cross-platform (Windows, macOS, Linux)