Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parsePhoneNumberWithError metadata argument was passed but it's not a valid metadata #468

Open
m2xilon opened this issue Sep 4, 2024 · 2 comments

Comments

@m2xilon
Copy link

m2xilon commented Sep 4, 2024

Getting an error when calling parsePhoneNumberWithError(mobileNumber, countryCode) in google cloud functions pubsub (using Cloud Functions v1)
libphonenumber-js versions affected: 1.11.3, 1.11.7

When: Encountered it only yesterday but unsure when exactly it was introduced, though must have been recent.

What: This is the error we have been getting:
[libphonenumber-js] metadataargument was passed but it's not a valid metadata. Must be an object having.countries child object property. Got an object of shape: { default }. at validateMetadata (/workspace/node_modules/.pnpm/[email protected]/node_modules/libphonenumber-js/source/metadata.js:463:9) at Metadata (/workspace/node_modules/.pnpm/[email protected]/node_modules/libphonenumber-js/source/metadata.js:23:3) at parse (/workspace/node_modules/.pnpm/[email protected]/node_modules/libphonenumber-js/source/parse.js:72:13) at parsePhoneNumberWithError (/workspace/node_modules/.pnpm/[email protected]/node_modules/libphonenumber-js/source/parsePhoneNumberWithError_.js:4:9) at parsePhoneNumberWithError (/workspace/node_modules/.pnpm/[email protected]/node_modules/libphonenumber-js/source/parsePhoneNumberWithError.js:6:9) at call (/workspace/node_modules/.pnpm/[email protected]/node_modules/libphonenumber-js/min/index.cjs.js:14:14) at parsePhoneNumberWithError (/workspace/node_modules/.pnpm/[email protected]/node_modules/libphonenumber-js/min/index.cjs.js:31:9)

Code:

import { parsePhoneNumberWithError } from 'libphonenumber-js'
export const validatedMobileNumber = (input: string, countryCode: CountryCode) => {
try {
    const phoneNumber = parsePhoneNumberWithError(input, countryCode) // error occurs here
  } catch (error) {
    logger?.info(`Error when parsing ${input} with parsePhoneNumberWithError`, error.message)
    throw error
  }
  // code never reaches here
  return phoneNumber.format('E.164')
}

This same code when running from a js script with node works as expected. But when deployed throws the error.

A workaround for a related package has been found by @nikita.rybak here (see comment from 23rd of August): https://gitlab.com/catamphetamine/react-phone-number-input/-/issues/141

Using this

import { parsePhoneNumberWithError } from 'libphonenumber-js/core'
import phoneNumberMetadata from 'libphonenumber-js/mobile/metadata'
...
phoneNumber = parsePhoneNumberWithError(input, countryCode, phoneNumberMetadata)
...

Presently an issue for some using as per the readme.

@catamphetamine
Copy link
Owner

catamphetamine commented Sep 4, 2024 via email

@mikecbrant
Copy link

mikecbrant commented Nov 19, 2024

Encountered same issue when updating from 1.10.48 to 1.11.14 without any changes to our code.

In our case usage is via parsePhoneNumber as follows:

const formattedNumber = parsePhoneNumber(String(number), 'US').number

We are using Typescript, which apparently want to try to use the node_modules/libphonenumber-js/min/index.cjs file.

I can confirm that the metadata that is requireed into this file is improperly formatted. If I place a log entry into this file near beginning like shown below...

'use strict'

var metadata = require('../metadata.min.json')
console.warn('METADATA', metadata)

Then the logged value for metadata is { default: [Getter] }. So it seems it is trying to JSON formatted for ECMA modules within the CJS file.

I see you have an interoperability hack a few lines below this for parsePhoneNumberFromString. Perhaps similar is needed for metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants