algolia / algoliasearch-client-javascript

⚡️ A fully-featured and blazing-fast JavaScript API client to interact with Algolia.
https://www.algolia.com/doc/api-client/javascript/getting-started/
MIT License
1.33k stars 222 forks source link

[bug]: `algoliasearch` fails with `tsx` and ESM ("type": "module") #1565

Closed karlhorky closed 1 month ago

karlhorky commented 1 month ago

I originally reported this over here, but the "skipLibCheck": false was a red herring for my particular issue (still leaving it open in case it's interesting on its own): https://github.com/algolia/algoliasearch-client-javascript/issues/1564


Description

CodeSandbox: https://codesandbox.io/p/devbox/p6w2mq?file=%2F.codesandbox%2Ftasks.json%3A12%2C15

Running algoliasearch with tsx and ESM ("type": "module") will cause a crash

  1. Create an ESM project with "type": "module" in package.json
  2. Add algoliasearch and tsx
  3. Create a index.ts file with a simple named import of algoliasearch
  4. Run pnpm tsx index.ts
  5. 💥 Observe the crash with the error message below

Screenshot 2024-10-18 at 18 03 22

cc @shortcuts wonder if this is related to https://github.com/algolia/api-clients-automation/pull/3966 (considering that the error is in a .ts file)

Client

Search

Version

5.9.1

Relevant log output

pnpm tsx index.ts
/project/workspace/node_modules/.pnpm/algoliasearch@5.9.1/node_modules/algoliasearch/builds/models.ts:19
import { EventType } from '@algolia/client-personalization';
         ^

SyntaxError: The requested module '@algolia/client-personalization' does not provide an export named 'EventType'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:217:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.12.0
nfarina commented 1 month ago

This is blocking our deploys and reverting to earlier versions doesn't seem to help. Not sure why yet - I didn't even upgrade the algoliasearch package and this just started happening on its own. Doesn't make sense but putting it out there.

Reverting to 5.8.1 did fix it for now. (our deploy script wasn't respecting the package.json resolutions field we use locally)

shortcuts commented 1 month ago

Hey @karlhorky thanks for reporting the issue and the investigation!!

I thought it was due to some treeshaking removing unused deps but leaving type imports, but you are right we shouldn't publish this typescript file!

edit: although I wonder why it worked before 🤔 I'll continue the investigation

sgilberg commented 1 month ago

It looks like this fix has been released, but I'm still seeing these compiling errors, any suggestions? I'm seeing them in a slightly different place from what's shown here and in issue #1566:


ERROR in ./node_modules/algoliasearch/dist/browser.js 22:19-28
export 'EventType' (imported as 'EventType') was not found in '@algolia/client-personalization' (possible exports: apiClientVersion, personalizationClient)

ERROR in ./node_modules/algoliasearch/dist/browser.js 23:16-22
export 'Status' (imported as 'Status') was not found in '@algolia/client-abtesting' (possible exports: abtestingClient, apiClientVersion)

ERROR in ./node_modules/algoliasearch/dist/browser.js 40:19-28
export 'EventType' (imported as 'EventType') was not found in '@algolia/client-personalization' (possible exports: apiClientVersion, personalizationClient)

ERROR in ./node_modules/algoliasearch/dist/browser.js 41:16-22
export 'Status' (imported as 'Status') was not found in '@algolia/client-abtesting' (possible exports: abtestingClient, apiClientVersion)

ERROR in ./node_modules/algoliasearch/dist/browser.js 140:0-145:2
export 'EventType' (reexported as 'EventType') was not found in '@algolia/client-personalization' (possible exports: apiClientVersion, personalizationClient)

ERROR in ./node_modules/algoliasearch/dist/browser.js 140:0-145:2
export 'Status' (reexported as 'Status') was not found in '@algolia/client-abtesting' (possible exports: abtestingClient, apiClientVersion)```
karlhorky commented 1 month ago

@shortcuts @Haroenv thanks for the release of algoliasearch@5.10.0 and algoliasearch@5.10.1, but as @sgilberg mentioned, the problem is not resolved.

I updated my reproduction to both versions and the same output appears:

CodeSandbox: https://codesandbox.io/p/devbox/algoliasearch-tsx-forked-d9q3d8?file=%2Fpackage.json

$ pnpm tsx index.ts

/project/workspace/node_modules/.pnpm/algoliasearch@5.10.1/node_modules/algoliasearch/builds/models.ts:19
import { EventType } from '@algolia/client-personalization';
         ^

SyntaxError: The requested module '@algolia/client-personalization' does not provide an export named 'EventType'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:217:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.12.0

Can this issue be reopened?

shortcuts commented 1 month ago

Hey @sgilberg @karlhorky I finally found the issue, thanks to your stacktraces and reproduction!

seems like omitting the type in the import statement prevents our bundler from removing the type imports from the outputted files, so you endup with ts definitions in js files.. see https://www.npmjs.com/package/algoliasearch?activeTab=code L53-L54

karlhorky commented 3 weeks ago

@shortcuts I can confirm that this is now resolved in algoliasearch@5.10.2 (via https://github.com/algolia/api-clients-automation/pull/4020 ) 🎉

CodeSandbox: https://codesandbox.io/p/devbox/algoliasearch-tsx-forked-wdgllg?file=%2Fpackage.json

$ pnpm start

> nodejs-sandbox@1.0.0 start /project/workspace
> tsx index.ts

algoliasearch [Function: algoliasearch]
shortcuts commented 3 weeks ago

lets gooooo thanks @karlhorky for testing!