Online Playground
Browse tools

How to install libphonenumber-js

Install libphonenumber-js with npm, yarn or a CDN. Compare the min, max and mobile builds with measured bundle sizes, and see TypeScript and import examples.

libphonenumber-js is a JavaScript library for parsing, formatting and validating phone numbers. It is a smaller rewrite of Google's libphonenumber that works in browsers and Node.js. This guide covers every way to install it and how to pick the right metadata build.

Install with npm or yarn

Use your package manager. The package ships its own TypeScript types, so you do not need a separate @types package.

bash
npm install libphonenumber-js
# or
yarn add libphonenumber-js
# or
pnpm add libphonenumber-js

Import it

The default build is the lightest one, called "min". Import the functions you need by name. Bundlers can then drop everything else.

js
import { parsePhoneNumberFromString } from 'libphonenumber-js'

const phone = parsePhoneNumberFromString('+44 20 7946 0958')
phone.isValid()       // true
phone.format('E.164') // '+442079460958'

Pick a metadata build: min, max or mobile

The library needs data about every country's numbering plan. You choose how much of it to ship by changing the import path. The sizes below are the complete browser bundles (code plus data) of version 1.13.15, which we measured ourselves from the npm package.

BuildImport pathSizeGzippedWhat you get
min (default)'libphonenumber-js'175 KB42 KBParse, format and length checks. isValid() behaves like isPossible(). Number type for only some countries.
max'libphonenumber-js/max'246 KB62 KBStrict validation and number type (mobile, fixed line, toll-free) for every country.
mobile'libphonenumber-js/mobile'190 KB46 KBStrict validation and types for mobile numbers. Other numbers still parse, but isValid() may return false and getType() may return nothing.
  • min is the default. Choose it when you do not need number types and a length check is enough, which covers most signup forms.
  • Switch to max when you need getType() for every country, or the strictest isValid().
  • Use mobile when you only accept mobile numbers and want to save bytes.

What min does not check

The min build keeps only the basic rules. The library's documentation says that with min, isValid() behaves the same as isPossible(): it checks the length of a number, but for most countries it does not check the digits against the exact assigned ranges. The same number can therefore be valid in min and invalid in max. We ran both builds on the same input to show it.

js
import parseMin from 'libphonenumber-js/min'
import parseMax from 'libphonenumber-js/max'

// India: 10 digits starting with 1 is not an assigned range
parseMin('+91 1000000000').isValid() // true  (length fits)
parseMax('+91 1000000000').isValid() // false (digits not in a real range)

// Germany: a real mobile number
parseMin('+49 151 23456789').getType() // undefined: min has no type data here
parseMax('+49 151 23456789').getType() // 'MOBILE'

Use it from a CDN without a build step

For a plain HTML page, load the standalone bundle. It exposes a global named libphonenumber.

html
<script src="https://unpkg.com/libphonenumber-js@1.13.15/bundle/libphonenumber-max.js"></script>
<script>
  const phone = libphonenumber.parsePhoneNumberFromString('+442079460958')
  console.log(phone.country) // "GB"
</script>

Use it in Node.js

It works in both ES modules and CommonJS. With CommonJS use require.

js
const { parsePhoneNumberFromString } = require('libphonenumber-js')
console.log(parsePhoneNumberFromString('(415) 555-2671', 'US').number)
// +14155552671

Try it before you install

The playground on this site runs the max build in your browser. Type a number and see the exact object the library returns, with the code that produces it.

FAQ

Questions people ask

Which libphonenumber-js build should I use?

Use the default min build unless you need strict digit validation or number types. min is the default build. Use max for getType() and the strictest isValid(), and mobile if you only accept mobile numbers.

Why does getType() return undefined?

You are probably on the min build, which has type data for only some countries. Import from libphonenumber-js/max to get the type for every country.

Does libphonenumber-js work without Node.js?

Yes. It is plain JavaScript with no native dependencies, so it runs in any browser and in Deno, Bun and Node.js.

Is libphonenumber-js free?

Yes. It is open source under the MIT license.