Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { spawn } = require('node:child_process')
const { EOL } = require('node:os')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const pkgJson = require('@npmcli/package-json')
const { defaults, definitions, nerfDarts } = require('@npmcli/config/lib/definitions')
const { defaults, definitions, nerfDarts, proxyEnv } = require('@npmcli/config/lib/definitions')
const { log, output } = require('proc-log')
const BaseCommand = require('../base-cmd.js')
const { redact } = require('@npmcli/redact')
Expand Down Expand Up @@ -350,6 +350,21 @@ ${defData}
}

if (!long) {
const envVars = []

// Show proxy-related environment variables if they're set
for (const envVar of proxyEnv) {
if (process.env[envVar]) {
envVars.push(`; ${envVar} = ${JSON.stringify(process.env[envVar])}`)
}
}

if (envVars.length > 0) {
msg.push('; environment-related config', '')
msg.push(...envVars)
msg.push('')
}

msg.push(
`; node bin location = ${process.execPath}`,
`; node version = ${process.version}`,
Expand Down
43 changes: 43 additions & 0 deletions test/lib/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,49 @@ t.test('config list', async t => {
t.matchSnapshot(output, 'output matches snapshot')
})

t.test('config list with proxy environment variables', async t => {
const originalHTTP = process.env.HTTP_PROXY
const originalHTTPS = process.env.HTTPS_PROXY
const originalNO = process.env.NO_PROXY

t.teardown(() => {
if (originalHTTP !== undefined) {
process.env.HTTP_PROXY = originalHTTP
} else {
delete process.env.HTTP_PROXY
}
if (originalHTTPS !== undefined) {
process.env.HTTPS_PROXY = originalHTTPS
} else {
delete process.env.HTTPS_PROXY
}
if (originalNO !== undefined) {
process.env.NO_PROXY = originalNO
} else {
delete process.env.NO_PROXY
}
})

process.env.HTTP_PROXY = 'http://proxy.example.com:8080'
process.env.HTTPS_PROXY = 'https://secure-proxy.example.com:8443'
process.env.NO_PROXY = 'localhost,127.0.0.1'

const { npm, joinedOutput } = await loadMockNpm(t, {
prefixDir: {
'.npmrc': 'test=value',
},
})

await npm.exec('config', ['list'])

const output = joinedOutput()

t.match(output, 'HTTP_PROXY = "http://proxy.example.com:8080"')
t.match(output, 'HTTPS_PROXY = "https://secure-proxy.example.com:8443"')
t.match(output, 'NO_PROXY = "localhost,127.0.0.1"')
t.match(output, 'environment-related config')
})

t.test('config list --long', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
prefixDir: {
Expand Down
12 changes: 12 additions & 0 deletions workspaces/config/lib/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,22 @@ const nerfDarts = [
'username', // Does not have a config
]

// Environment variables that can affect npm's behavior but are not npm configs
// These are shown in `npm config list` to help users understand their environment
const proxyEnv = [
'HTTP_PROXY',
'HTTPS_PROXY',
'NO_PROXY',
'http_proxy',
'https_proxy',
'no_proxy',
]

module.exports = {
defaults: definitionProps.defaults,
definitions,
flatten,
nerfDarts,
proxyEnv,
shorthands,
}
Loading