Registry / communication / phoneformat.js

phoneformat.js

JSON →
library1.0.9jsnpmunverified

A JavaScript telephone number formatting library based on Google's libphonenumber and compiled via Google Closure. Version 1.0.9 is the latest, last synced with libphonenumber v8.10.6. Provides formatting (E164, international, local), validation, country code lookup, and examples. Note: it may lag behind the latest libphonenumber; users can manually update the source via the Closure compiler. Provides AMD, CommonJS, and global builds. Lightweight alternative to libphonenumber-js or google-libphonenumber.

npm install phoneformat.js
INSTALL
IMPORT
SIG · PHONEFORMAT.JS
P
phoneformat.js
communicationjavascriptv1.0.9
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
✓ import phoneFormat from 'phoneformat.js'
✗ const phoneFormat = require('phoneformat.js');
In Node.js or bundlers, the package exports a single object with all methods. Use default import (ESM) or require (CJS). In browsers, use the global build and access `phoneFormat` on window.
countryForE164Number
✓ import phoneFormat from 'phoneformat.js'; const result = phoneFormat.countryForE164Number('+14155552671');
✗ import { countryForE164Number } from 'phoneformat.js'; const result = countryForE164Number('+14155552671');
All functions are properties of the default export object. Named exports are not available.
formatE164
✓ import phoneFormat from 'phoneformat.js'; const formatted = phoneFormat.formatE164('US', '4155552671');
✗ const phoneFormat = require('phoneformat.js').default; const formatted = phoneFormat.formatE164('US', '4155552671');
In CommonJS, the default export is the entire module. Do not append `.default`.

Demonstrates importing phoneFormat.js and using its key functions: validation, E164/international/local formatting, mobile dialing, and cleaning.

import phoneFormat from 'phoneformat.js'; // Validate and format a US number const number = '4155552671'; const country = 'US'; // Check if valid const isValid = phoneFormat.isValidNumber(number, country); console.log('Is valid:', isValid); // Format to E164 const e164 = phoneFormat.formatE164(country, number); console.log('E164:', e164); // Format to international const international = phoneFormat.formatInternational(country, number); console.log('International:', international); // Format to local const local = phoneFormat.formatLocal(country, number); console.log('Local:', local); // Phone number for mobile dialing const mobileDial = phoneFormat.formatNumberForMobileDialing(country, number); console.log('Mobile dial:', mobileDial); // Clean phone (strip non-digits) const cleaned = phoneFormat.cleanPhone('+1 (415) 555-2671'); console.log('Cleaned:', cleaned);
Debug
Known issues
deprecatedNot compatible with latest libphonenumber; last synced v8.10.6.
fix
Manually update source via Closure compiler per README or switch to actively maintained library like libphonenumber-js or google-libphonenumber.
affects: >=1.0.0
gotchaAll functions are on the default export object; named imports do not work.
fix
Use default import: import phoneFormat from 'phoneformat.js'; then access methods as properties.
affects: >=1.0.0
gotchaCommonJS require does not need .default; the module.exports is the phoneFormat object directly.
fix
Use const phoneFormat = require('phoneformat.js'); not require(...).default.
affects: >=1.0.0
gotchaCountry codes must be uppercase ISO 3166-1 alpha-2 (e.g., 'US', 'GB'). Lowercase may not work.
fix
Always provide country codes in uppercase (e.g., 'US' not 'us').
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: phoneFormat.isValidNumber is not a function
Using named import instead of default import.
fix
Change import statement to: import phoneFormat from 'phoneformat.js'; then use phoneFormat.isValidNumber().
phoneformat.js:1 Uncaught ReferenceError: module is not defined
Using CommonJS build in a browser environment without a bundler.
fix
Use the global build (phone-format-global.js) instead of the CommonJS one.
Error: countryForE164Number expects a valid international number string
Passing a national number without country code or invalid number.
fix
Ensure the phone number is in E164 format (e.g., +14155552671) and valid.
Upgrade
Version history
1.0.9latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
phoneformat.js — npm install phoneformat.js · libregistry