Skip to content

Commit 545e861

Browse files
Max Blackwraithgar
authored andcommitted
feat: show proxy environment variables in npm config list
Adds proxy-related environment variables to npm config list output to help users understand their proxy configuration. Variables are checked case-insensitively to match the agent library behavior. Closes #4170
1 parent 5a444d5 commit 545e861

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lib/commands/config.js

Lines changed: 18 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,23 @@ ${defData}
350350
}
351351

352352
if (!long) {
353+
const envVars = []
354+
355+
const foundEnvVars = new Set()
356+
for (const key of Object.keys(process.env)) {
357+
const lowerKey = key.toLowerCase()
358+
if (proxyEnv.includes(lowerKey) && !foundEnvVars.has(lowerKey)) {
359+
foundEnvVars.add(lowerKey)
360+
envVars.push(`; ${key} = ${JSON.stringify(process.env[key])}`)
361+
}
362+
}
363+
364+
if (envVars.length > 0) {
365+
msg.push('; environment-related config', '')
366+
msg.push(...envVars)
367+
msg.push('')
368+
}
369+
353370
msg.push(
354371
`; node bin location = ${process.execPath}`,
355372
`; 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)