elastic / elasticsearch-js

Official Elasticsearch client library for Node.js
https://ela.st/js-client
Apache License 2.0
30 stars 731 forks source link

ESM import issue (replicated with Deno v2 and Node 23) #2466

Closed eturino closed 1 week ago

eturino commented 2 weeks ago

🐛 Bug report

With v8.16.0 in a deno v2 project, we run into the following problem when running:

error: Could not resolve 'npm:@elastic/elasticsearch@8.16.0'.

Caused by:
    [ERR_INVALID_PACKAGE_TARGET] Invalid "exports" main target {"require":"./index.js"} defined in the package config /workspaces/entities-api/node_modules/.deno/@elastic+elasticsearch@8.16.0/node_modules/@elastic/elasticsearch/package.json imported from 'file:///workspaces/entities-api/src/search-api/es-client.ts'; target must start with "./"

For what I can see, the issue is in the exports section of the package.json. Probably should be "exports": { ".": "./index.js" }, (replace "require" with ".")

The issue is not present with 8.15.x versions, which are commonjs packages entirely.

To reproduce

In a deno v2 (potentially also other ESM runtimes), install version 8.16.0 and run your app and/or any tests that uses a file that imports the es library.

Expected behavior

Should be imported and run as expected

Node.js version

Deno v2

@elastic/elasticsearch version

8.16.0

Operating system

Debian bookworm

Any other relevant environment information

No response

ffMathy commented 2 weeks ago

This is not just Deno. Just happened on Node 23 as well for me. The new *.16 release is broken.

eturino commented 2 weeks ago

I expected so, but I only tried it in deno.

ffMathy commented 2 weeks ago

@JoshMock I am mentioning you here, as I believe you'd be interested in knowing that the latest version doesn't work at all. It can't even be imported.

ajkraus04 commented 2 weeks ago

This is crazy, how not a single javascript user can use elastic right now, this should be a urgent issue!!!!!!!

sankoshine commented 2 weeks ago

So it's really a bug? Any workaround on this?

ajkraus04 commented 2 weeks ago

Just down grade to 7.16.10 or whatever the prior major version was and that will fix issue for now.

zurmokeeper commented 2 weeks ago

@eturino You can refer to this PR #2467 for a temporary solution, or downgrade to a version before 8.16.0.

hitmands commented 2 weeks ago
[commonjs--resolver] Failed to resolve entry for package "@elastic/elasticsearch". 
The package may have incorrect main/module/exports specified in its package.json: 
No known conditions for "." specifier in "@elastic/elasticsearch" package transforming (51) ../index.ts

A quick turnaround would be greatly appreciated.

statox commented 2 weeks ago

I think @ajkraus04 advice is wrong: the issue comes from 8.16.0 not from 8.x. And 7.x clients are not compatible with 8.x clusters.

So before 8.16.x is fixed, depending on the version of your cluster you should set the package version to 8.15.2 instead ("@elastic/elasticsearch": "8.15.2" in package.json)

alexey-sh commented 1 week ago

This yarn add @elastic/elasticsearch@8.15.2 solves the following error

import { ErrorCause } from "@elastic/elasticsearch/lib/api/types";

error TS2307: Cannot find module '@elastic/elasticsearch/lib/api/types' or its corresponding type declarations.

16 import { ErrorCause } from "@elastic/elasticsearch/lib/api/types";
siketyan commented 1 week ago

We need types entry for TypeScript as well:

{
  "exports": {
    ".": {
      "types": "./index.d.ts",
      "default": "./index.js"
    }
  }
}
JoshMock commented 1 week ago

Thanks for your patience, everyone. I've opened https://github.com/elastic/elasticsearch-js/pull/2475 with a fix, as well as a smoke test to ensure ECMAScript imports continue to work going forward. I will publish a patch release 8.16.1 today with the fix.

This was only was broken for 8.16.0, so downgrading to 8.15.x is a temporary fix. It was also only broken for code using native ECMAScript import syntax, so the client was working as expected for those still using CommonJS require syntax.

JoshMock commented 1 week ago

8.16.1 has been published with the ESM import fix.

augustozanellato commented 1 week ago

@JoshMock I'm still noticing weird behaviours on 8.16.1, the following fails despite working on 8.15 and previous releases.

import type {
    SearchRequest,
    SearchResponse
} from '@elastic/elasticsearch/lib/api/types';

Specifically it fails with TS2307: Cannot find module '@elastic/elasticsearch/lib/api/types' or its corresponding type declarations.

siketyan commented 1 week ago

@augustozanellato lib/api/types seems not intended to be used outside the package. You can use estypes barrel export instead:

import type {estypes} from '@elastic/elasticsearch';

let req: estypes.SearchRequest;
augustozanellato commented 1 week ago

@augustozanellato lib/api/types seems not intended to be used outside the package. You can use estypes barrel export instead:

import type {estypes} from '@elastic/elasticsearch';

let req: estypes.SearchRequest;

That indeed solved my issue, thanks.

JoshMock commented 1 week ago

Yep, estypes is the best way to handle it for now. I do want to ensure maximum flexibility, though, so I'll look into adding exports for more files.