Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js throws ERR_INVALID_PROTOCOL when calling an https URL

ErrorTypeError [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.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3The options object has protocol: 'https:' but the call goes through http.requestA protocol option set to something the module does not matchFix

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.js

Confirmed by nodejs.org

Did this fix it?

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.js

Confirmed by nodejs.org

Did this fix it?

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

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.