Skip to content

Commit 9c29072

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 9c29072

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

lib/commands/config.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { spawn } = require('node:child_process')
44
const { EOL } = require('node:os')
55
const localeCompare = require('@isaacs/string-locale-compare')('en')
66
const pkgJson = require('@npmcli/package-json')
7-
const { defaults, definitions, nerfDarts } = require('@npmcli/config/lib/definitions')
7+
const { defaults, definitions, nerfDarts, proxyEnv } = require('@npmcli/config/lib/definitions')
88
const { log, output } = require('proc-log')
99
const BaseCommand = require('../base-cmd.js')
1010
const { redact } = require('@npmcli/redact')
@@ -350,6 +350,21 @@ ${defData}
350350
}
351351

352352
if (!long) {
353+
const envVars = []
354+
355+
// Show proxy-related environment variables if they're set
356+
for (const envVar of proxyEnv) {
357+
if (process.env[envVar]) {
358+
envVars.push(`; ${envVar} = ${JSON.stringify(process.env[envVar])}`)
359+
}
360+
}
361+
362+
if (envVars.length > 0) {
363+
msg.push('; environment-related config', '')
364+
msg.push(...envVars)
365+
msg.push('')
366+
}
367+
353368
msg.push(
354369
`; node bin location = ${process.execPath}`,
355370
`; 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: {

workspaces/config/lib/definitions/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,22 @@ const nerfDarts = [
7171
'username', // Does not have a config
7272
]
7373

74+
// Environment variables that can affect npm's behavior but are not npm configs
75+
// These are shown in `npm config list` to help users understand their environment
76+
const proxyEnv = [
77+
'HTTP_PROXY',
78+
'HTTPS_PROXY',
79+
'NO_PROXY',
80+
'http_proxy',
81+
'https_proxy',
82+
'no_proxy',
83+
]
84+
7485
module.exports = {
7586
defaults: definitionProps.defaults,
7687
definitions,
7788
flatten,
7889
nerfDarts,
90+
proxyEnv,
7991
shorthands,
8092
}

0 commit comments

Comments
 (0)