privatenumber/tsx

Fix in 4.16.3 broke use of certain libraries when allowJs is true

lawrenceong opened this issue · 6 comments

Acknowledgements

  • I read the documentation and searched existing issues to avoid duplicates
  • I understand this is a bug tracker and anything other than a proven bug will be closed
  • I understand this is a free project and relies on community contributions
  • I read and understood the Contribution guide

Minimal reproduction URL

https://stackblitz.com/edit/node-epykqv

Problem & expected behavior (under 200 words)

From and including version 4.16.3 of tsx, the bug caused the following problem:

If tsconfig.json contains a compiler option of allowJs set to true:

{
  "compilerOptions": {
    "allowJs": true
  }
}

Then certain packages will fail when used with tsx. From the minimal reproduction URL, using libphonenumber-js and tsx, a simple call will result in the following error:

> tsx index.ts

Error: [libphonenumber-js] `metadata` argument 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 (/home/projects/node-epykqv/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/build/metadata.js:591:11)
    at new Metadata (/home/projects/node-epykqv/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/build/metadata.js:43:5)
    at parse (/home/projects/node-epykqv/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/build/parse.js:89:14)
    at parsePhoneNumberWithError (/home/projects/node-epykqv/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/build/parsePhoneNumberWithError_.js:19:32)
    at parsePhoneNumber (/home/projects/node-epykqv/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/build/parsePhoneNumber_.js:32:55)
    at parsePhoneNumber (/home/projects/node-epykqv/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/build/parsePhoneNumber.js:20:44)
    at call (/home/projects/node-epykqv/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/min/index.cjs.js:14:14)
    at parsePhoneNumberFromString (/home/projects/node-epykqv/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/min/index.cjs.js:18:9)
    at eval (/home/projects/node-epykqv/index.ts:2:864)
    at Object.eval (/home/projects/node-epykqv/index.ts:3:3)

index.ts:

import parsePhoneNumber from 'libphonenumber-js';

console.log(parsePhoneNumber('+61491570006').isValid());

Bugs are expected to be fixed by those affected by it

  • I'm interested in working on this issue

Compensating engineering work will speed up resolution and support the project

  • I'm willing to offer $10 for financial support

I am seeing this same issue. I discovered this after recently upgrading to TSX v4.17 from an earlier version.

At line 10 in libphonenumber-js/index.cjs.js they load a json file using CommonJS require syntax. This ends up with an object with the default property set to an accessor to the json object instead of the json object itself.

The line is:
var metadata = require('./metadata.min.json')

For now, I have to rollback to TSX v4.16.2.

Thanks for the issue and minimal reproduction.

Looks like the TS resolver is trying the .js extension to handle the implicit extension case, and it happened to match, which ends up loading the wrong file.

Will try to look into a fix later this week.

If anyone is interested in helping, feel free to contribute constructively by identifying the problem and proposing solutions.