libphonenumber-bundler CLI
This package provides a Command Line Interface (CLI) utility designed to compile and bundle Google's official `libphonenumber` JavaScript port into a single, standalone JavaScript file. Unlike modern alternatives like `libphonenumber-js` which are independent rewrites, this tool directly uses the original Closure-compiled JavaScript output from Google's project, aiming for a faithful reproduction of its full functionality, including its potentially larger footprint and reliance on Closure's API patterns. The current stable version, 0.4.0, has not seen updates in over two years, indicating a maintenance status for the bundler tool itself, although it can fetch recent versions of the upstream `libphonenumber` source code. Its key differentiator is simplifying the process of obtaining a self-contained, official `libphonenumber` bundle without requiring a complex build setup, making it suitable for projects that prefer a direct script inclusion or a CommonJS module of the original Google library rather than a streamlined rewrite.
Common errors
-
lpn-bundler: command not found
cause The `libphonenumber-bundler` package was installed locally or not added to the system's PATH.fixInstall the package globally using `npm install -g libphonenumber-bundler` or ensure your shell's PATH includes `$(npm bin)`. -
Error: Version not found: -t X.Y.Z
cause The specified `libphonenumber` version tag (e.g., 'X.Y.Z') does not exist or cannot be fetched from the upstream Google repository.fixRun `lpn-bundler list` to see all available versions and use one from the displayed list. Ensure you have network connectivity to GitHub. -
TypeError: Cannot read properties of undefined (reading 'getInstance')
cause The `libphonenumber` object or `PhoneNumberUtil` class was not properly loaded or is not available in the current scope when attempting to call `getInstance()` (e.g., script not included, wrong variable name, or using a module system incorrectly with a global-style bundle).fixIn a browser, ensure the generated `libphonenumber.js` script is loaded *before* your code attempts to use `libphonenumber.PhoneNumberUtil`. In Node.js, ensure you are correctly requiring the file and accessing the `PhoneNumberUtil` export (e.g., `const { PhoneNumberUtil } = require('./libphonenumber.js');`).
Warnings
- maintenance The `libphonenumber-bundler` package itself (version 0.4.0) has not been updated in over two years, indicating it is no longer actively maintained. While it can fetch recent versions of the upstream `libphonenumber` source, the bundler tool itself may contain unaddressed bugs or security vulnerabilities.
- gotcha The generated bundle is from Google's Closure-compiled `libphonenumber` JavaScript port. This port can be significantly larger than optimized rewrites (e.g., `libphonenumber-js`) and is tightly coupled to Google Closure's API patterns, which may differ from standard JavaScript module exports.
- gotcha Phone number metadata is frequently updated by Google's `libphonenumber` project. To ensure accurate parsing and validation, users must periodically re-run the `lpn-bundler bundle` command to incorporate the latest upstream metadata, as the CLI tool does not automatically update generated files.
- gotcha The `libphonenumber-bundler` CLI generates a standalone JavaScript file without offering explicit options for modern module formats (ESM, UMD, CJS wrappers). The output typically assumes a global scope for browser use or a basic CommonJS export for Node.js.
Install
-
npm install libphonenumber-bundler -
yarn add libphonenumber-bundler -
pnpm add libphonenumber-bundler
Imports
- libphonenumber.PhoneNumberUtil
import { PhoneNumberUtil } from 'libphonenumber-bundler'// After including the bundled script (e.g., in browser): const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
- libphonenumber.PhoneNumberFormat
import { PhoneNumberFormat } from 'libphonenumber-bundler'// After including the bundled script (e.g., in browser): const PNF = libphonenumber.PhoneNumberFormat;
- require('path/to/libphonenumber.js').PhoneNumberUtil
import { PhoneNumberUtil } from 'libphonenumber-bundler'// In Node.js, assuming the bundled file is 'libphonenumber.js' in the current directory: const { PhoneNumberUtil } = require('./libphonenumber'); const phoneUtil = PhoneNumberUtil.getInstance();
Quickstart
npm install -g libphonenumber-bundler
# List available libphonenumber versions to bundle
lpn-bundler list
# Bundle a specific version of libphonenumber (e.g., 8.12.57) to a file
lpn-bundler bundle -t 8.12.57 -o libphonenumber.js
# Example usage of the generated file (libphonenumber.js) in a browser:
// <script src="libphonenumber.js"></script>
// <script>
// const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
// const number = phoneUtil.parse('+12024561414', 'US');
// console.log(phoneUtil.isValidNumber(number));
// console.log(phoneUtil.format(number, libphonenumber.PhoneNumberFormat.INTERNATIONAL));
// </script>