HTTP URL String Validator
This package provides a focused utility function to determine if a given string adheres to the structural requirements of an HTTP or HTTPS URL. Currently at version 1.0.3, it is a stable, low-cadence library, primarily maintained for compatibility and reliability rather than frequent feature additions. Its core validation logic is built upon a robust regular expression initially developed by Diego Perini, which is well-regarded for its comprehensive coverage of various URL components. Unlike larger, more feature-rich URL parsing libraries, `is-valid-http-url` offers a lightweight and direct API specifically for string format validation, making it ideal for scenarios where only a quick check for valid HTTP/HTTPS URL syntax is needed without additional overhead.
Common errors
-
TypeError: (0 , is_valid_http_url_1.default) is not a function
cause Attempting to use a named import (`import { isUrl } from '...'`) for a package that provides a default export in an ESM context.fixChange the import statement to `import isUrl from 'is-valid-http-url';` for ESM environments. -
TypeError: isUrl is not a function
cause Occurs when `require()` is used on a module that primarily expects ESM default imports, or if the imported name does not match the exported symbol.fixEnsure you are using `const isUrl = require('is-valid-http-url');` in CommonJS, or `import isUrl from 'is-valid-http-url';` in ESM, as the package provides a default function export.
Warnings
- gotcha This library only validates the *format* of an HTTP/HTTPS URL string. It does not perform DNS lookups, check for server availability, or verify the existence of the resource at the given URL.
- gotcha The validator is specifically designed for HTTP and HTTPS protocols. It will return `false` for URLs using other schemes like `ftp://`, `mailto:`, `file://`, etc., even if their format is otherwise correct for their respective schemes.
- gotcha The validation logic is based on a regular expression. While robust, extremely rare or highly unusual URL formats might not be perfectly covered. Ensure URLs conform to standard RFCs for best results.
Install
-
npm install is-valid-http-url -
yarn add is-valid-http-url -
pnpm add is-valid-http-url
Imports
- isUrl
import { isUrl } from 'is-valid-http-url';import isUrl from 'is-valid-http-url';
- isUrl
const isUrl = require('is-valid-http-url');
Quickstart
import isUrl from 'is-valid-http-url';
// Test cases for valid HTTP/HTTPS URLs
console.log('Valid URLs:');
console.log(`http://example.com: ${isUrl('http://example.com')}`);
console.log(`https://www.example.com/foo/?bar=baz&inga=42&quux: ${isUrl('https://www.example.com/foo/?bar=baz&inga=42&quux')}`);
console.log(`http://localhost:3000: ${isUrl('http://localhost:3000')}`);
console.log(`https://user:pass@host.com/path?query#hash: ${isUrl('https://user:pass@host.com/path?query#hash')}`);
console.log('\nInvalid URLs:');
// Test cases for invalid URLs
console.log(`example.com (missing scheme): ${isUrl('example.com')}`);
console.log(`http://foo.bar?q=Spaces should be encoded: ${isUrl('http://foo.bar?q=Spaces should be encoded')}`);
console.log(`ftp://example.com (non-http scheme): ${isUrl('ftp://example.com')}`);
console.log(`invalid-url: ${isUrl('invalid-url')}`);
console.log(`
Note: This validator checks syntax, not if the URL actually exists or is reachable.`);