Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch and Angular 2

I'm using Angular 2 with angular-cli and I installed the elasticsearch library with

nom install --save elasticsearch

There is an elasticsearch directory in node_modules.

I do the import with

import * as elasticsearch from 'elasticsearch';

As I understand this is a relative path that will resolve to the node_modules path and angular-cli (with ng serve) will take care of bundling everything properly.

However I get the following error:

ERROR in [default] /Users/xxx/yyy/src/app/shared/elasticsearch/es.service.ts:4:31 
Cannot find module 'elasticsearch'.

Any help would be appreciated.

I confirmed that the problem appears with an empty project generated with ng init.

Also, doing something like this:

var elasticsearch = require('elasticsearch');

works well.

like image 646
Cedric H. Avatar asked Mar 23 '26 00:03

Cedric H.


1 Answers

The simplest way to get this working, is to first install the type definitions:

npm install --save-dev @types/elasticsearch

And then, armed with your new shiny types, you can use import statements like this one:

import { Client } from 'elasticsearch';

... and use it like this:

let x = new Client({ /* Your parameters here */});
like image 89
Cobus Kruger Avatar answered Mar 24 '26 12:03

Cobus Kruger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!