Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
eturino opened this issue Nov 14, 2024 · 17 comments · Fixed by #2475
Closed

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

eturino opened this issue Nov 14, 2024 · 17 comments · Fixed by #2475

Comments

@eturino
Copy link

eturino commented Nov 14, 2024

🐛 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/[email protected]'.

Caused by:
    [ERR_INVALID_PACKAGE_TARGET] Invalid "exports" main target {"require":"./index.js"} defined in the package config /workspaces/entities-api/node_modules/.deno/@[email protected]/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
Copy link

ffMathy commented Nov 14, 2024

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

@eturino
Copy link
Author

eturino commented Nov 14, 2024

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

@eturino eturino changed the title ESM import issue (replicated with Deno v2) ESM import issue (replicated with Deno v2 and Node 23) Nov 14, 2024
@ffMathy
Copy link

ffMathy commented Nov 14, 2024

@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
Copy link

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

@sankoshine
Copy link

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

@ajkraus04
Copy link

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

@zurmokeeper
Copy link

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

@hitmands
Copy link

hitmands commented Nov 15, 2024

[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
Copy link

statox commented Nov 15, 2024

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
Copy link

This yarn add @elastic/[email protected] 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
Copy link

We need types entry for TypeScript as well:

{
  "exports": {
    ".": {
      "types": "./index.d.ts",
      "default": "./index.js"
    }
  }
}

@JoshMock
Copy link
Member

Thanks for your patience, everyone. I've opened #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
Copy link
Member

8.16.1 has been published with the ESM import fix.

@augustozanellato
Copy link

@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
Copy link

siketyan commented Nov 20, 2024

@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
Copy link

@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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.