Node.js throws ERR_INVALID_PROTOCOL when calling an https URL
TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"Short answer
The http module only speaks http:. Calling http.request or http.get with an https:// URL, or passing an agent built for the other protocol, raises ERR_INVALID_PROTOCOL. Use https.request or https.get for https URLs, and give them an https.Agent, never an http.Agent.
Fix it fast
1Find out which cause you have
Find which module and which agent make the failing call. The error points at the line that calls http.request or http.get.
grep -rn --include=*.js --include=*.mjs -E "require\('(node:)?http'\)|from '(node:)?http'|new (http\.)?Agent" src/2Match what you see, then jump to its fix
- 1
The stack trace ends in http.request or http.get while the URL starts with https://The http module is used for an https:// URLFix - 2
The https module is already in use, but the options contain agent: new http.Agent(...) or a proxy agent made for plain HTTPAn http.Agent is passed to a request for an https URLFix - 3
The options object has protocol: 'https:' but the call goes through http.requestA protocol option set to something the module does not matchFix
- 1
What does the error look like?
TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"ERR_INVALID_PROTOCOL
Cause 1 of 3
The http module is used for an https:// URL
1Check
http.request defaults protocol to 'http:' and port to 80. https.request defaults to 'https:' and port 443.
grep -rn -E "http\.(get|request)\(" src/2Fix
Import the https module for secure URLs. Its request and get accept the same options as the http ones.
const https = require('node:https');
https.get('https://example.com/', (res) => {
console.log(res.statusCode);
res.resume();
}).on('error', console.error);3Verify
The request prints a status code instead of throwing.
node request.jsConfirmed by nodejs.org
Cause 2 of 3
An http.Agent is passed to a request for an https URL
1Check
An Agent has a protocol option that defaults to 'http:'. A request whose protocol differs from the agent's protocol is rejected.
grep -rn -E "agent\s*[:=]" src/2Fix
Create an https.Agent for https requests, or let Node.js use https.globalAgent by leaving agent out.
const https = require('node:https');
const agent = new https.Agent({ keepAlive: true });
https.get('https://example.com/', { agent }, (res) => {
console.log(res.statusCode);
res.resume();
});3Verify
Run the same call again. The status code prints and no ERR_INVALID_PROTOCOL appears.
node request.jsConfirmed by nodejs.org
Cause 3 of 3
A protocol option set to something the module does not match
1Check
ERR_INVALID_PROTOCOL means an invalid options.protocol was passed to http.request(). The protocol in the options has to match the module you call.
grep -rn "protocol" src/2Fix
Drop the protocol option and pick the module by scheme, so the URL decides which one runs.
const http = require('node:http');
const https = require('node:https');
function get(url, cb) {
const lib = new URL(url).protocol === 'https:' ? https : http;
return lib.get(url, cb);
}3Verify
Call get() once with an http:// URL and once with an https:// URL. Both answer.
node -e "require('./get.js')"Confirmed by nodejs.org
How do I stop it coming back?
- Pick the module from the URL scheme in one helper instead of scattering http and https imports.
- Reuse one https.Agent per process rather than building agents inline.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix