URL Parse As Address
url-parse-as-address is a JavaScript utility for parsing URL strings, specifically designed to assume the `http` protocol if no scheme or leading `//` is explicitly provided. This behavior mimics how web browsers handle incomplete URL inputs, making it suitable for scenarios where user-entered addresses might lack full protocol specification (e.g., 'foo.com' instead of 'http://foo.com'). Published as version 1.0.0 over a decade ago, this package primarily functions as a CommonJS module. It provides a parsed object structure similar to Node.js's legacy `url.parse()` method, including properties like `protocol`, `host`, `pathname`, and `query`. Due to its age and lack of updates, newer projects are generally advised to use the WHATWG `URL` API (built into Node.js and modern browsers) or more actively maintained alternatives that offer better security, features, and ESM support, especially given its limited ecosystem adoption (17 dependents).
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ECMAScript Module (ESM) file.fixThis package is CommonJS-only. If your project is ESM, either convert the file to CommonJS (`.cjs` or `"type": "commonjs"` in `package.json`) or use `import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);` to create a `require` function in ESM. It's better to migrate away from this package. -
TypeError: parse is not a function (or similar when trying to import { parse })cause Incorrect ES module `import` syntax used for a CommonJS package that exports a default function.fixUse `const parse = require('url-parse-as-address');` as the package exports its main function as the `module.exports` object. It does not have named exports for `parse` or `format` in the ESM sense.
Warnings
- deprecated This package is very old (last updated over 11 years ago) and is considered abandoned. It has not received updates for security vulnerabilities, bug fixes, or compatibility with modern JavaScript environments. Its parsing logic may not fully align with current URL specifications or edge cases handled by modern URL parsers.
- gotcha The package is a CommonJS module and does not support ES module `import` syntax directly. Attempting to `import` it in an ESM environment will cause a runtime error.
- gotcha The primary differentiation of this library is its assumption of 'http:' protocol for URLs without a scheme (e.g., 'foo.com'). While this is useful for browser-like input, it deviates from the strict parsing of the WHATWG `URL` API, which requires a valid base URL for relative or protocol-less inputs. This can lead to different parsing results compared to modern standards.
Install
-
npm install url-parse-as-address -
yarn add url-parse-as-address -
pnpm add url-parse-as-address
Imports
- parse
import parse from 'url-parse-as-address'; // OR import { parse } from 'url-parse-as-address';const parse = require('url-parse-as-address'); - parse.parse
import { parse } from 'url-parse-as-address/parse'; // Incorrect subpath importconst parse = require('url-parse-as-address'); const parsedUrl = parse.parse('foo.com'); - parse.format
import { format } from 'url-parse-as-address'; // 'format' is not a named exportconst parse = require('url-parse-as-address'); const formattedUrl = parse.format({ protocol: 'http:', hostname: 'foo.com' });
Quickstart
const parse = require('url-parse-as-address');
const assert = require('assert');
const urlString = 'foo.com:1234/x?y=z#a=b';
// Basic parsing, query string not parsed into an object
assert.deepEqual(parse(urlString),
{ protocol: 'http:',
slashes: true,
auth: null,
host: 'foo.com:1234',
port: '1234',
hostname: 'foo.com',
hash: '#a=b',
search: '?y=z',
query: 'y=z',
pathname: '/x',
path: '/x?y=z',
href: 'http://foo.com:1234/x?y=z#a=b' });
// Parsing with query string as an object (second argument `true`)
assert.deepEqual(parse(urlString, true),
{ protocol: 'http:',
slashes: true,
auth: null,
host: 'foo.com:1234',
port: '1234',
hostname: 'foo.com',
hash: '#a=b',
search: '?y=z',
query: { y: 'z' },
pathname: '/x',
path: '/x?y=z',
href: 'http://foo.com:1234/x?y=z#a=b' });
console.log('All assertions passed!');