Prepend HTTP/S Protocol to URLs

4.0.0 · active · verified Wed Apr 22

prepend-http is a focused utility package designed to ensure URLs have a proper `http://` or `https://` protocol prefix. It intelligently adds `https://` (by default) to "humanized" URLs such as domain names (`sindresorhus.com`) or `localhost` entries, while leaving already-prefixed URLs untouched. The package is currently stable at version `4.0.0`, released recently in response to ecosystem changes. Historically, releases are infrequent but significant, often tied to Node.js LTS updates or module system shifts. A key differentiator is its simplicity and reliability, provided by a well-regarded maintainer in the JavaScript ecosystem. Since version 4, prepend-http is a pure ESM package, aligning with modern JavaScript module practices, and requires Node.js 12.20 or newer.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `prependHttp` with various URL inputs, including defaulting to `https`, explicitly using `http`, and handling already-prefixed URLs.

import prependHttp from 'prepend-http';

// Example 1: Basic usage - prepends https:// by default
const url1 = 'sindresorhus.com';
const prefixedUrl1 = prependHttp(url1);
console.log(`'${url1}' -> '${prefixedUrl1}'`);
// Expected: 'sindresorhus.com' -> 'https://sindresorhus.com'

// Example 2: Explicitly prepend http://
const url2 = 'localhost:3000';
const prefixedUrl2 = prependHttp(url2, { https: false });
console.log(`'${url2}' -> '${prefixedUrl2}'`);
// Expected: 'localhost:3000' -> 'http://localhost:3000'

// Example 3: URL already has a protocol - should be untouched
const url3 = 'ftp://example.com/file.txt';
const prefixedUrl3 = prependHttp(url3);
console.log(`'${url3}' -> '${prefixedUrl3}'`);
// Expected: 'ftp://example.com/file.txt' -> 'ftp://example.com/file.txt'

// Example 4: Mixed case - ensure consistency
const url4 = 'http://github.com';
const prefixedUrl4 = prependHttp(url4);
console.log(`'${url4}' -> '${prefixedUrl4}'`);
// Expected: 'http://github.com' -> 'http://github.com'

view raw JSON →