This error occurs when your Node.js application or npm tries to connect through a proxy server that requires authentication credentials. The request is rejected because no valid username and password were provided to the proxy.
The HTTP 407 Proxy Authentication Required error indicates that your Node.js application is attempting to make an HTTP/HTTPS request through a corporate or network proxy server, but the proxy requires authentication credentials that weren't provided or were invalid. Unlike a 401 Unauthorized error (which comes from the destination server), a 407 response comes from the intermediate proxy server itself. The proxy sits between your application and the internet, and it won't forward your request until you prove you're authorized to use it. This is extremely common in corporate environments where all internet traffic must pass through authenticated proxy servers for security, monitoring, or policy enforcement purposes. The proxy server sends back a 407 status code along with a Proxy-Authenticate header that specifies which authentication methods it supports (Basic, Digest, NTLM, or custom schemes).
Set your proxy configuration with credentials directly in npm:
npm config set proxy http://username:[email protected]:8080
npm config set https-proxy http://username:[email protected]:8080If your password contains special characters (@, !, $, #, etc.), URL-encode them:
- @ becomes %40
- ! becomes %21
- # becomes %23
- $ becomes %24
Example with special characters:
npm config set proxy http://john%40doe:P%40ssw0rd%[email protected]:8080You can also use an online URL encoder or Node.js:
const encoded = encodeURIComponent('P@ssw0rd!');
console.log(encoded); // P%40ssw0rd%21Configure environment variables that Node.js and many libraries respect:
Linux/macOS (bash/zsh):
export HTTP_PROXY=http://username:[email protected]:8080
export HTTPS_PROXY=http://username:[email protected]:8080
export NO_PROXY=localhost,127.0.0.1,.company.com
export NODE_USE_ENV_PROXY=1Add these to ~/.bashrc or ~/.zshrc for persistence.
Windows (Command Prompt):
set HTTP_PROXY=http://username:[email protected]:8080
set HTTPS_PROXY=http://username:[email protected]:8080
set NO_PROXY=localhost,127.0.0.1
set NODE_USE_ENV_PROXY=1Windows (PowerShell):
$env:HTTP_PROXY="http://username:[email protected]:8080"
$env:HTTPS_PROXY="http://username:[email protected]:8080"
$env:NO_PROXY="localhost,127.0.0.1"
$env:NODE_USE_ENV_PROXY="1"The NODE_USE_ENV_PROXY flag (Node.js v22.21.0+) enables native proxy support for http, https, and fetch().
Create a .npmrc file in your project root or home directory:
proxy=http://username:[email protected]:8080
https-proxy=http://username:[email protected]:8080
strict-ssl=trueFor security, avoid committing credentials to version control. Use environment variables instead:
.npmrc
proxy=http://${PROXY_USER}:${PROXY_PASS}@proxy.company.com:8080
https-proxy=http://${PROXY_USER}:${PROXY_PASS}@proxy.company.com:8080Then set environment variables:
export PROXY_USER=your-username
export PROXY_PASS=your-passwordFor applications making HTTP requests, configure the proxy programmatically:
Using native fetch() (Node.js 18+):
// Enable environment proxy support
process.env.NODE_USE_ENV_PROXY = '1';
// fetch() will automatically use HTTP_PROXY/HTTPS_PROXY
const response = await fetch('https://api.example.com/data');Using https.request() with proxy-agent:
npm install proxy-agentimport { ProxyAgent } from 'proxy-agent';
const agent = new ProxyAgent({
getProxyForUrl: () => 'http://username:[email protected]:8080'
});
const options = {
hostname: 'api.example.com',
path: '/data',
agent: agent
};
https.request(options, (res) => {
// Handle response
});Using axios:
npm install axiosimport axios from 'axios';
const response = await axios.get('https://api.example.com/data', {
proxy: {
protocol: 'http',
host: 'proxy.company.com',
port: 8080,
auth: {
username: 'your-username',
password: 'your-password'
}
}
});Old or incorrect credentials can cause persistent 407 errors even after updating configuration.
Windows - Clear Credential Manager:
1. Open Control Panel → Credential Manager
2. Look under "Windows Credentials" and "Generic Credentials"
3. Remove any entries related to your proxy server
4. Restart your terminal and try again
macOS - Clear Keychain:
1. Open Keychain Access app
2. Search for your proxy server name
3. Delete any related entries
4. Restart your terminal
Verify npm configuration:
npm config get proxy
npm config get https-proxyTest the configuration:
npm ping
# or
npm install express --verboseThe --verbose flag shows exactly what npm is doing and where it fails.
NTLM/Kerberos Authentication:
Corporate proxies often use NTLM (Windows-integrated) or Kerberos authentication, which npm and Node.js don't support natively. Solutions include:
1. CNTLM Proxy: Install CNTLM (http://cntlm.sourceforge.net/), a local authentication proxy that handles NTLM:
# Configure CNTLM with your credentials
cntlm -H -u username -d DOMAIN
# Then point npm to localhost
npm config set proxy http://localhost:31282. Px Proxy: A Python-based alternative (https://github.com/genotrance/px):
pip install px-proxy
px --proxy=proxy.company.com:8080
npm config set proxy http://localhost:3128Security Considerations:
- Never commit proxy credentials to version control
- Use environment variables or secure credential storage
- Consider using a .env file with dotenv library:
import 'dotenv/config';
const proxyUrl = `http://${process.env.PROXY_USER}:${process.env.PROXY_PASS}@proxy.company.com:8080`;- Add .env and .npmrc to .gitignore
Self-Signed Certificates:
If your proxy uses a self-signed certificate, you may need:
npm config set strict-ssl false
# or
export NODE_TLS_REJECT_UNAUTHORIZED=0⚠️ Warning: Disabling SSL verification reduces security. Only use in trusted corporate environments.
Proxy Auto-Configuration (PAC) Files:
Some organizations use PAC files for dynamic proxy configuration. Node.js doesn't support PAC files natively. Use the pac-proxy-agent package:
npm install pac-proxy-agentimport { PacProxyAgent } from 'pac-proxy-agent';
const agent = new PacProxyAgent('http://proxy.company.com/proxy.pac');Troubleshooting Tips:
- Check if your proxy requires domain\username format: DOMAIN\username or [email protected]
- Some proxies log authentication failures - contact your IT team with timestamps
- Test proxy connectivity outside Node.js: curl -x http://username:[email protected]:8080 https://registry.npmjs.org
- Use --loglevel verbose or --loglevel silly with npm commands for detailed debugging
Error: Listener already called (once event already fired)
EventEmitter listener already called with once()
Error: EACCES: permission denied, open '/root/file.txt'
EACCES: permission denied
Error: Invalid encoding specified (stream encoding not supported)
How to fix Invalid encoding error in Node.js readable streams
Error: EINVAL: invalid argument, open
EINVAL: invalid argument, open
TypeError: readableLength must be a positive integer (stream config)
TypeError: readableLength must be a positive integer in Node.js streams