3

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.

6
  • @LauraAbadAvilés Is this an answer to my question or another question? Commented Nov 22, 2016 at 12:02
  • It was a question, but I figure it out. client = new elasticsearch.Client({ host: 'localhost:9200', //log: 'trace' }); Commented Nov 23, 2016 at 8:06
  • @LauraAbadAvilés Yes indeed. What about my question and Angular2, how did you import the library? Commented Nov 23, 2016 at 16:45
  • I imported same as you: ngOnInit(){ this.es = require('elasticsearch') } Commented Nov 24, 2016 at 11:11
  • Did you ever solve this? I have the same issue. Commented Jan 24, 2017 at 18:55

1 Answer 1

2

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 */});
Sign up to request clarification or add additional context in comments.

Comments

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.