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

algoliasearch v5 not building in a typescript project #1538

Closed OutdatedGuy closed 2 months ago

OutdatedGuy commented 3 months ago

Steps to reproduce:

  1. Create a typescript project (preferably a firebase cloud function project)
  2. Add algoliasearch with below command
    npm install algoliasearch@5
  3. Build the project using tsc command
  4. See below error

    > tsc
    
    node_modules/algoliasearch/dist/algoliasearch/builds/node.d.ts:2:48 - error TS2307: Cannot find module '@algolia/client-abtesting/src/abtestingClient' or its corresponding type declarations.
    
    2 import type { Region as AbtestingRegion } from '@algolia/client-abtesting/src/abtestingClient';
                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    node_modules/algoliasearch/dist/algoliasearch/builds/node.d.ts:4:48 - error TS2307: Cannot find module '@algolia/client-analytics/src/analyticsClient' or its corresponding type declarations.
    
    4 import type { Region as AnalyticsRegion } from '@algolia/client-analytics/src/analyticsClient';
                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    node_modules/algoliasearch/dist/algoliasearch/builds/node.d.ts:7:54 - error TS2307: Cannot find module '@algolia/client-personalization/src/personalizationClient' or its corresponding type declarations.
    
    7 import type { Region as PersonalizationRegion } from '@algolia/client-personalization/src/personalizationClient';
                                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Found 3 errors in the same file, starting at: node_modules/algoliasearch/dist/algoliasearch/builds/node.d.ts:2

Things Already Tried

shortcuts commented 3 months ago

Hey @OutdatedGuy thanks for trying out the v5 client

I've not been able to reproduce the error, it compiles correctly on my side.

However from the error you shared, it seems like the peer dependencies of algoliasearch are not installed. If they do not resolve automatically for you, it might be an exports issue on our side.

Was it working with v4? You can also try to install them manually (@algolia/client-personalization and so on), or use the umd bundled file which contains them, usually for the browser env import {algoliasearch} from "algoliasearch/dist/algoliasearch.umd.js"

OutdatedGuy commented 3 months ago

@shortcuts

Was it working with v4?

Yes. On v4 it was working flawlessly but since v5 I cannot build my project :(

You can also try to install them manually (@algolia/client-personalization and so on)

I installed the dependencies mentioned in the error logs with:

npm install @algolia/client-personalization @algolia/client-analytics @algolia/client-abtesting;

but still the same error occurs.

e-compa commented 3 months ago

Hi,

we've encountered the same error, and we were able to work around it by modifying _node_modules/.pnpm/algoliasearch@5.1.1/nodemodules/algoliasearch/dist/algoliasearch/builds/node.d.ts

from this:

import type { AbtestingClient } from '@algolia/client-abtesting';
import type { Region as AbtestingRegion } from '@algolia/client-abtesting/src/abtestingClient';
import type { AnalyticsClient } from '@algolia/client-analytics';
import type { Region as AnalyticsRegion } from '@algolia/client-analytics/src/analyticsClient';
import type { ClientOptions } from '@algolia/client-common';
import type { PersonalizationClient } from '@algolia/client-personalization';
import type { Region as PersonalizationRegion } from '@algolia/client-personalization/src/personalizationClient';
import type { RecommendClient } from '@algolia/recommend';
import type { InitClientOptions, InitClientRegion, GenerateSecuredApiKeyOptions, GetSecuredApiKeyRemainingValidityOptions } from './models';
export * from './models';
export declare const apiClientVersion = "5.1.1";

to this:

import type { AbtestingClient } from '@algolia/client-abtesting';
import type { Region as AbtestingRegion } from '@algolia/client-abtesting/dist/src/abtestingClient';
import type { AnalyticsClient } from '@algolia/client-analytics';
import type { Region as AnalyticsRegion } from '@algolia/client-analytics/dist/src/analyticsClient';
import type { ClientOptions } from '@algolia/client-common';
import type { PersonalizationClient } from '@algolia/client-personalization';
import type { Region as PersonalizationRegion } from '@algolia/client-personalization/dist/src/personalizationClient';
import type { RecommendClient } from '@algolia/recommend';
import type { InitClientOptions, InitClientRegion, GenerateSecuredApiKeyOptions, GetSecuredApiKeyRemainingValidityOptions } from './models';
export * from './models';
export declare const apiClientVersion = "5.1.1";

since the import path does not match the folder structure

zapobyte commented 2 months ago

I have also managed to reproduce this using version "5.1.1".

shortcuts commented 2 months ago

Hey there, thanks for providing more context to the issue!

Could you please let me know if the latest version fixes the issue on your side?

OutdatedGuy commented 2 months ago

@shortcuts I tried with the latest version (5.2.1) but having the same issue. So I looked into the error file and saw imports whose paths don't exist (@algolia/client-abtesting/src/abtestingClient). So I just updated code to import from base package and it worked.

Basically:

- import type { AbtestingClient } from '@algolia/client-abtesting';
- import type { Region as AbtestingRegion } from '@algolia/client-abtesting/src/abtestingClient';
+ import type { AbtestingClient, Region as AbtestingRegion } from '@algolia/client-abtesting';

Can you re-open the issue?

shortcuts commented 2 months ago

@shortcuts I tried with the latest version (5.2.1) but having the same issue. So I looked into the error file and saw imports whose paths don't exist (@algolia/client-abtesting/src/abtestingClient). So I just updated code to import from base package and it worked.

Basically:

- import type { AbtestingClient } from '@algolia/client-abtesting';
- import type { Region as AbtestingRegion } from '@algolia/client-abtesting/src/abtestingClient';
+ import type { AbtestingClient, Region as AbtestingRegion } from '@algolia/client-abtesting';

Can you re-open the issue?

Hey, thanks for testing it! it was fixed in 5.2.2, could you retry please?

OutdatedGuy commented 2 months ago

Hi, I tried 5.2.2 and it worked. Thanks.

shortcuts commented 2 months ago

Lets goooo enjoy!! Thanks you

valeriangalliat commented 1 month ago

Encountering the same issue with algoliasearch@5.7.0

../../node_modules/@algolia/client-common/src/transporter/helpers.ts(12,5): error TS2322: Type 'TData | undefined' is not assignable to type 'TData'.
  'TData' could be instantiated with an arbitrary type which could be unrelated to 'TData | undefined'.
../../node_modules/@algolia/client-common/src/transporter/helpers.ts(13,5): error TS2322: Type 'TData | undefined' is not assignable to type 'TData'.
  'TData' could be instantiated with an arbitrary type which could be unrelated to 'TData | undefined'.
../../node_modules/@algolia/client-common/src/transporter/helpers.ts(72,5): error TS2322: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.
husky - pre-commit hook exited with code 1 (error)

It seems TypeScript (5.5.4 but also tried with 5.6.2 (latest)) doesn't respect the package.json exports field and still reads node_modules/@algolia/client-common/index.ts and its subtree from src/ instead of the dist/ files.

Removing node_modules/@algolia/client-common/index.ts solves the issue

Mandy9943 commented 1 month ago

Same problem as above here image

Im using ^5.7.0

shortcuts commented 1 month ago

hey @Mandy9943 @valeriangalliat it seems related to https://github.com/algolia/algoliasearch-client-javascript/issues/1561, which should be fixed with https://github.com/algolia/api-clients-automation/pull/3966

HemalR commented 1 month ago

I think I am getting the same (or a similar) error:

No matching export in "node_modules/@algolia/client-abtesting/dist/builds/browser.js" for import "Status"

I can see that all the dependencies of algoliasearch (V 5.9.1) are installed.

image

Not sure how to fix or what to try. Open to suggestions. (Please let me know if I should create a new issue for this).

Edit to add link to reproduction of bug:

https://stackblitz.com/edit/sveltejs-kit-template-default-qmp5ae?file=src%2Froutes%2F%2Bpage.svelte

sureshkumarsendrayan commented 1 month ago

@5.7.0

I'm also facing the same issue.