Skip to content

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

@m2xilon

Description

@m2xilon

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/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/metadata.js:463:9) at Metadata (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/metadata.js:23:3) at parse (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/parse.js:72:13) at parsePhoneNumberWithError (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/parsePhoneNumberWithError_.js:4:9) at parsePhoneNumberWithError (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/parsePhoneNumberWithError.js:6:9) at call (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/min/index.cjs.js:14:14) at parsePhoneNumberWithError (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions