add third party lib like elasticsearch
Please provide us with the following information:
OS?
Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?)
MacOS Sierra
Versions.
Please run
ng --version. If there's nothing outputted, please run in a Terminal:node --versionand paste the result here:
ng --version Could not start watchman; falling back to NodeWatcher for file system events. Visit http://ember-cli.com/user-guide/#watchman for more info. angular-cli: 1.0.0-beta.16 node: 6.5.0 os: darwin x64
Repro steps.
Was this an app that wasn't created using the CLI? What change did you do on your code? etc. The app was created with the CLI ng new ng2_es cd ng2_es npm install elasticsearch-browser --save npm install @types/elasticsearch --save-dev
then I edited the app.component.ts and added one line:
import elasticsearch from 'elasticsearch';
and launched ng serve
The log given by the failure.
Normally this include a stack trace and some more information.
ERROR in ./src/app/app.component.ts Module not found: Error: Can't resolve 'elasticsearch' in '/Users/Calopea/Documents/CALOPEA/development/js/ng2_es/src/app' @ ./src/app/app.component.ts 11:0-42 @ ./src/app/index.ts @ ./src/main.ts @ multi main
ERROR in [default] /Users/Calopea/Documents/CALOPEA/development/js/ng2_es/src/app/app.component.ts:3:7 Module '"elasticsearch"' has no default export.
Mention any other details that might be useful.
I might be missing a very simple step, let me know if you need anything more.
Thanks!
Thanks! We'll be in touch soon.
Try import * as elasticsearch from 'elasticsearch';
Hi, thanks @deebloo for the suggestion. I tried and I get another kind of error:
ERROR in ./src/app/app.component.ts Module not found: Error: Can't resolve 'elasticsearch' in '/Users/Calopea/Documents/CALOPEA/development/js/ng2_es/src/app' @ ./src/app/app.component.ts 11:0-47 @ ./src/app/index.ts @ ./src/main.ts @ multi main
ERROR in [default] /Users/Calopea/Documents/CALOPEA/development/js/ng2_es/src/app/app.component.ts:12:33 Module 'Elasticsearch' has no exported member 'ClientInterface'.
ERROR in [default] /Users/Calopea/Documents/CALOPEA/development/js/ng2_es/src/app/app.component.ts:15:19 Value of type 'typeof Client' is not callable. Did you mean to include 'new'?
I pushed the scaffolded code to a repository: https://github.com/ldt/ng2_es
And I continue to investigate the error.
I was successful to add JQuery library, you can try:
- Run
npm install jquery --saveandnpm install @types/jquery --save-dev - Add
types: ["jquery"]in filesrc/tsconfig.json - Add declaration
declare var $: JQueryStaticin filesrc/typings.d.ts - Add
import * as $ from 'jquery'from where you want to use
Thanks @kiddy2910, I've just given a try. I skipped your steps 2 and 3 and it worked perfectly. I just need to find out how to make it work for other libs.
I made it working and updated the sample in the repo: https://github.com/ldt/ng2_es
3 changes:
- used the elasticsearch lib instead of elasticsearch-browser (in package.json)
"dependencies": {
"elasticsearch": "^11.0.1",
- removed the script import in angular-cli.json
"apps": [
{
(...)
"scripts": [
],
- updated the app.component.ts to use it based on the elasticsearch quick start
import { Component, OnInit } from '@angular/core';
import * as elasticsearch from 'elasticsearch';
import * as $ from 'jquery'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app works!';
private _client: elasticsearch.Client
constructor() {
this._client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
}
ngOnInit() {
$('h1').text('jquery and elasticsearch libs work!');
this._client.ping({
requestTimeout: 30000,
// undocumented params are appended to the query string
hello: "elasticsearch"
}, function (error) {
if (error) {
console.error('elasticsearch cluster is down!');
} else {
console.log('All is well');
}
});
}
}
So, apologies as it was all my bad.
I followed the above guideline, but wasnt able to successfully import elasticsearch.
It is giving me this error :
Uncaught (in promise): Error: Arguments array must have arguments.
Anyone has any idea about this?
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.