Skip to content

Commit f0b2737

Browse files
author
Max Black
committed
feat: show proxy environment variables in npm config list
Fixes #4170 pm config list now displays proxy-related environment variables (HTTP_PROXY, HTTPS_PROXY, NO_PROXY and their lowercase equivalents) when they are set. This helps users understand what proxy settings are being used by npm. The environment variables are shown in a separate '; environment-related config' section before the system information (node version, npm version, etc.).
1 parent dd104da commit f0b2737

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

lib/commands/config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,22 @@ ${defData}
350350
}
351351

352352
if (!long) {
353+
const envVars = []
354+
355+
// Show proxy-related environment variables if they're set
356+
const proxyEnvVars = ['HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY', 'http_proxy', 'https_proxy', 'no_proxy']
357+
for (const envVar of proxyEnvVars) {
358+
if (process.env[envVar]) {
359+
envVars.push(`; ${envVar} = ${JSON.stringify(process.env[envVar])}`)
360+
}
361+
}
362+
363+
if (envVars.length > 0) {
364+
msg.push('; environment-related config', '')
365+
msg.push(...envVars)
366+
msg.push('')
367+
}
368+
353369
msg.push(
354370
`; node bin location = ${process.execPath}`,
355371
`; node version = ${process.version}`,

test/lib/commands/config.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,49 @@ t.test('config list', async t => {
101101
t.matchSnapshot(output, 'output matches snapshot')
102102
})
103103

104+
t.test('config list with proxy environment variables', async t => {
105+
const originalHTTP = process.env.HTTP_PROXY
106+
const originalHTTPS = process.env.HTTPS_PROXY
107+
const originalNO = process.env.NO_PROXY
108+
109+
t.teardown(() => {
110+
if (originalHTTP !== undefined) {
111+
process.env.HTTP_PROXY = originalHTTP
112+
} else {
113+
delete process.env.HTTP_PROXY
114+
}
115+
if (originalHTTPS !== undefined) {
116+
process.env.HTTPS_PROXY = originalHTTPS
117+
} else {
118+
delete process.env.HTTPS_PROXY
119+
}
120+
if (originalNO !== undefined) {
121+
process.env.NO_PROXY = originalNO
122+
} else {
123+
delete process.env.NO_PROXY
124+
}
125+
})
126+
127+
process.env.HTTP_PROXY = 'http://proxy.example.com:8080'
128+
process.env.HTTPS_PROXY = 'https://secure-proxy.example.com:8443'
129+
process.env.NO_PROXY = 'localhost,127.0.0.1'
130+
131+
const { npm, joinedOutput } = await loadMockNpm(t, {
132+
prefixDir: {
133+
'.npmrc': 'test=value',
134+
},
135+
})
136+
137+
await npm.exec('config', ['list'])
138+
139+
const output = joinedOutput()
140+
141+
t.match(output, 'HTTP_PROXY = "http://proxy.example.com:8080"')
142+
t.match(output, 'HTTPS_PROXY = "https://secure-proxy.example.com:8443"')
143+
t.match(output, 'NO_PROXY = "localhost,127.0.0.1"')
144+
t.match(output, 'environment-related config')
145+
})
146+
104147
t.test('config list --long', async t => {
105148
const { npm, joinedOutput } = await loadMockNpm(t, {
106149
prefixDir: {

0 commit comments

Comments
 (0)