Registry / productivity / phonenumber-formatter

phonenumber-formatter

JSON →
library1.3.0jsnpmunverified

A simple browser-side phone number formatter for HTML input elements. Current version 1.3.0 is stable but appears to have no recent updates. It only supports 10-digit US-style numbers and does not provide validation beyond formatting. Unlike robust libraries like libphonenumber, this package is minimal and targeted at basic input masking.

npm install phonenumber-formatter
INSTALL
IMPORT
SIG · PHONENUMBER-FORMAT
P
phonenumber-formatter
productivityjavascriptv1.3.0
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.

formatPhonenumbers
✓ import formatPhonenumbers from 'phonenumber-formatter'
✗ const formatPhonenumbers = require('phonenumber-formatter')
The library does not ship TypeScript types. 'require' works in CommonJS environments, but ESM import is preferred for modern bundlers.
default
✓ import formatPhonenumbers from 'phonenumber-formatter'
✗ import { formatPhonenumbers } from 'phonenumber-formatter'
The module exports a single default function, not a named export. Named import will result in undefined.
require
✓ const formatPhonenumbers = require('phonenumber-formatter')
✗ const { formatPhonenumbers } = require('phonenumber-formatter')
Destructuring the require result will also yield undefined, as the export is the function itself, not an object.

Demonstrates how to import the default function and apply it to an input element after DOM ready.

import formatPhonenumbers from 'phonenumber-formatter'; document.addEventListener('DOMContentLoaded', () => { const input = document.getElementById('phone'); if (input) { formatPhonenumbers(input); } });
Debug
Known issues
breakingThe library only supports 10-digit US phone numbers. Entering numbers of other lengths or international formats will result in unexpected formatting or no formatting.
fix
Consider using libphonenumber-js or google-libphonenumber for international support.
affects: >=1.0.0
gotchaThe function expects an HTMLInputElement, not a string. Passing a string will cause errors or no formatting.
fix
Pass the DOM element directly: formatPhonenumbers(document.getElementById('phone')).
affects: >=1.0.0
gotchaThe library does not provide TypeScript type definitions. Using in a TypeScript project may require a custom declaration file.
fix
Create a .d.ts file: declare module 'phonenumber-formatter' { const formatPhonenumbers: (el: HTMLInputElement, callback?: () => void) => void; export default formatPhonenumbers; }
affects: >=1.0.0
gotchaThe library has no validation; it will format any 10-digit input, but does not check for valid area codes or phone number patterns beyond digits.
fix
Add your own validation before or after formatting using a regex or validation library.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: formatPhonenumbers is not a function
Attempting named import instead of default import.
fix
Use 'import formatPhonenumbers from 'phonenumber-formatter'' instead of 'import { formatPhonenumbers } from 'phonenumber-formatter''.
undefined is not a function (evaluating 'formatPhonenumbers(...)')
Destructuring the require or import incorrectly, getting undefined instead of the function.
fix
Ensure default import: const formatPhonenumbers = require('phonenumber-formatter') and do not destructure.
Cannot read property 'value' of null
The function is called with an element that does not exist or before the DOM is ready.
fix
Guarantee the input exists and the DOM is loaded, e.g., by placing script at end of body or using DOMContentLoaded event.
Upgrade
Version history
1.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
24
Resources
phonenumber-formatter — npm install phonenumber-formatter · libregistry