privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
9.66k stars 154 forks source link

Weird import default error #86

Closed wmelton closed 2 years ago

wmelton commented 2 years ago

Bug description

When utilizing the @tensorflow-models/universal-sentence-encoder npm package, we experienced a weird bug that went unnoticed until deployment to production. We utilized esmo by @antfu for all local dev development on a typescript express api.

Using: import use from '@tensorflow-models/universal-sentence-encoder' in our script worked perfectly locally. After building (ts->cjs/esm) and deploying to production, we got errors essentially saying many of the module functions were undefined.

After messing with it, we discovered that updating the import statement to: import * as use from '@tensorflow-models/universal-sentence-encoder' fixed the issue both when running uncompiled ts from the cli and post-build js in our production environment.

I'm not familiar enough with the build process ts->cjs/esm to understand the details of what could cause this. However, on the face of it, it does seem to me that if an import would fail post-compile, then tsx should fail too.

Reproduction

  1. Build a test script that utilized import use from '@tensorflow-models/universal-sentence-encoder'
  2. Build ts code using tsc -p ./tsconfig.json
  3. Try to load the model: model = await use.load() in the resulting code
  4. `...undefined (reading 'load')
  5. Update import = import * as use from '@tensorflow-models/universal-sentence-encoder'
  6. repeat steps 2-3. Works.

Environment

System:
    OS: macOS 12.3.1
    CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
    Memory: 1.11 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    npm: 8.16.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
Packages:
├── @aws-sdk/client-s3@3.112.0
├── @datadog/datadog-api-client@1.0.0
├── @sentry/node@6.19.7
├── @sentry/tracing@6.19.7
├── @tensorflow-models/universal-sentence-encoder@1.3.3
├── @tensorflow/tfjs-node@3.18.0
├── @tsconfig/node16@1.0.3
├── @types/connect-datadog@0.0.6
├── @types/cookie-parser@1.4.3
├── @types/cors@2.8.12
├── @types/express@4.17.13
├── @types/jest@27.5.2
├── @types/mysql@2.15.21
├── @types/node@17.0.45
├── @types/sharp@0.30.4
├── @types/xml2js@0.4.11
├── @types/yup@0.29.14
├── @typescript-eslint/eslint-plugin@5.28.0
├── @typescript-eslint/parser@5.28.0
├── axios@0.26.1
├── bumpp@7.2.0
├── conventional-changelog-cli@2.2.2
├── cookie-parser@1.4.6
├── cors@2.8.5
├── dateformat@4.6.3
├── dd-trace@2.9.1
├── dotenv@16.0.1
├── eslint-config-prettier@8.5.0
├── eslint-import-resolver-typescript@2.7.1
├── eslint-plugin-eslint-comments@3.2.0
├── eslint-plugin-import@2.26.0
├── eslint-plugin-jsonc@2.3.0
├── eslint-plugin-unicorn@41.0.1
├── eslint@8.17.0
├── esno@0.14.1
├── express@4.18.1
├── form-data@4.0.0
├── google-libphonenumber@3.2.28
├── html-tablify@1.1.1
├── jest@27.5.1
├── mailgun.js@5.2.2
├── mkdir-recursive@0.4.0
├── mysql@2.18.1
├── nanoid@3.3.4
├── nodemon@2.0.19
├── request-ip@2.2.0
├── rimraf@3.0.2
├── sharp@0.30.7
├── shelljs@0.8.5
├── stripe-money-format@1.0.10
├── stripe@8.222.0
├── ts-jest@27.1.5
├── ts-node@10.8.1
├── tsc-watch@4.6.2
├── typescript@4.7.4
├── unbuild@0.7.4
├── util@0.12.4
├── winston-sentry@0.2.1
├── winston@3.8.1
├── xlsx@0.18.5
├── xml2js@0.4.23
└── yup@1.0.0-beta.4

Can you contribute a fix?

privatenumber commented 2 years ago

Thanks for the issue.

I can't reproduce though. Can you link to a repository containing a minimal reproduction?

wmelton commented 2 years ago

Yeah no problem, I will put that together and update with a link.

privatenumber commented 2 years ago

Will re-open once reproduction is provided

TehShrike commented 1 year ago

I have a minimal reproduction at https://github.com/TehShrike/tsx_issue_86_repro

I ran into the issue importing TypeScript files from a module.

privatenumber commented 1 year ago

Thanks for the reproduction @TehShrike

Your issue is the same as https://github.com/esbuild-kit/tsx/issues/38