Skip to content

Wrong use of CDN (libphonenumber-js) or CDN not working?

I am trying to use the library libphonenumber-js.

<body>

    <script src="https://unpkg.com/[email protected]/bundle/libphonenumber-min.js"></script>

    <script src="./js/main.js"></script>

</body>

In my main.js I tried to write a simple code which is from the docs of libphonenumber-js:

    const phoneNumber = parsePhoneNumber('+12133734253');

However I get this error. Same thing with other functions from the docs.

Uncaught ReferenceError: parsePhoneNumber is not defined

In my network tabs I can see that the min.js file is being downloaded. But its functions are not usable for some reason. I tried making a min.js file and using it as well but with not luck.

I also tried many different CDN services which also host this library.

Answer

For anyone wondering:

I found out in the issues page of the repo that you have to use window.libphonenumber or just libphonenumber in front of the function. So

window.libphonenumber.parsePhoneNumberFromString(
      '(213) 373-42-53 ext. 1234',
      'US'
    );

Also in my example turns out that the correct function was parsePhoneNumberFromString