is-url-http
is-url-http is a focused utility library for JavaScript and TypeScript environments that efficiently determines if a given string represents a valid HTTP or HTTPS URL. It primarily checks the scheme (http or https) and basic URL structure based on WHATWG URL Standard conventions. The current stable version is 2.3.13, and the library exhibits an active maintenance schedule with frequent patch and minor updates, typically every few weeks, addressing bug fixes and minor enhancements. Its key differentiators include its singular focus on HTTP/HTTPS URL validation, ensuring a small footprint and predictable behavior without delving into broader URL parsing concerns. A notable feature is the provision of a separate 'lightweight' bundle, specifically optimized for browser environments, which aims to minimize bundle size while retaining core validation functionality. This makes it an ideal choice for client-side applications where every byte counts, differentiating it from more comprehensive URL manipulation libraries.
Common errors
-
TypeError: (0 , is_url_http_1.default) is not a function
cause Attempting to use `isUrlHttp` as a named import (e.g., `import { isUrlHttp } from 'is-url-http';`) in an ESM context, when the library exports its main function as a default export.fixUse a default import instead: `import isUrlHttp from 'is-url-http';` -
TypeError: isUrlHttp is not a function
cause Attempting to destructure `isUrlHttp` from a `require` statement (e.g., `const { isUrlHttp } = require('is-url-http');`) in a CommonJS environment, when the module exports the function directly as `module.exports`.fixAssign the `require` call directly to the function variable: `const isUrlHttp = require('is-url-http');` -
Error: Cannot find module 'is-url-http/lightweight'
cause Incorrect module path specified for the lightweight bundle, or the dependency was not installed correctly.fixEnsure the import path is exactly `is-url-http/lightweight`. Verify `node_modules` structure and run `npm install` or `yarn install` to ensure all dependencies are correctly placed. If using bundlers, check alias configurations.
Warnings
- gotcha This library exclusively validates against `http://` and `https://` schemes, based on WHATWG URL Standard conventions. URLs with other protocols (e.g., `ftp://`, `mailto://`, `ws://`) will always return `false`, even if they are syntactically valid for their respective schemes. It does not perform network requests or check for URL reachability.
- gotcha The `is-url-http/lightweight` bundle is optimized for size, primarily for browser environments. While generally functionally equivalent for typical HTTP/HTTPS URLs, it might use a simplified underlying URL parsing mechanism derived from its `url-http` dependency. This could lead to subtle differences in edge-case handling (e.g., highly malformed URLs, specific internationalized domain names) compared to the full version.
Install
-
npm install is-url-http -
yarn add is-url-http -
pnpm add is-url-http
Imports
- isUrlHttp
import { isUrlHttp } from 'is-url-http';import isUrlHttp from 'is-url-http';
- isUrlHttp (CommonJS)
const { isUrlHttp } = require('is-url-http');const isUrlHttp = require('is-url-http'); - isUrlHttp (lightweight bundle)
import { isUrlHttpLight } from 'is-url-http/lightweight';import isUrlHttpLight from 'is-url-http/lightweight';
- Type of isUrlHttp function
import type IsUrlHttp from 'is-url-http';
Quickstart
import isUrlHttp from 'is-url-http';
import isUrlHttpLight from 'is-url-http/lightweight';
console.log('--- Standard Version ---');
console.log('Valid HTTPS:', isUrlHttp('https://kikobeats.com')); // Expected: true
console.log('Valid HTTP:', isUrlHttp('http://localhost:3000')); // Expected: true
console.log('Invalid FTP scheme:', isUrlHttp('ftp://example.com')); // Expected: false
console.log('Invalid Mailto scheme:', isUrlHttp('mailto:test@example.com')); // Expected: false
console.log('Malformed URL string:', isUrlHttp('invalid-url-string')); // Expected: false
console.log('\n--- Lightweight Version ---');
console.log('Valid HTTPS (light):', isUrlHttpLight('https://kikobeats.com')); // Expected: true
console.log('Valid HTTP (light):', isUrlHttpLight('http://localhost:3000')); // Expected: true
console.log('Invalid WS scheme (light):', isUrlHttpLight('ws://example.com')); // Expected: false
console.log('URL with trailing space (light):', isUrlHttpLight(' https://trailing-space.com ')); // Expected: true