2

I was attempting to use the JS library for elasticsearch in an Angular application. The library imported with no problem:

npm install elasticsearch
npm install @types/elasticsearch

And then the IDE could recognize the types for the API. However it would not work in the browser. Instead I had to use

npm install elasticsearch-browser

This works and now the API works but there is no @types/elasticsearch to install. I can proceed with development but it really helps to have the IDE check my code. Does anyone know if there is a type-definition out there somewhere?

A possible work-around is to somehow import the @types/elasticsearch definitions and use them for the elasticsearch-browser package. But I don't know how to do that. Any help?

1 Answer 1

4

I don't know if it's the right way, but this worked for me:

elasticsearch.d.ts:

declare module "elasticsearch-browser" {
  import elasticsearch from "elasticsearch";
  import "elasticsearch-browser";
  export * from "elasticsearch";
}

Then import elasticsearch from "elasticsearch-browser" anywhere you need to use it.

Sign up to request clarification or add additional context in comments.

2 Comments

You've saved my day. For new client lib, just change import elasticsearch from "elasticsearch"; to import "@elastic/elasticsearch"; and export * from "elasticsearch"; to export * from "@elastic/elasticsearch";.
@pootow which folder should the elasticsearch.d.ts be located? I just can't get elastic working in Angular. if you happen to have a small sample I'd be ever so grateful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.