angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

add third party lib like elasticsearch

Open ldt opened this issue 10 years ago • 7 comments

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 --version and 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.

ldt avatar Oct 04 '16 14:10 ldt

Try import * as elasticsearch from 'elasticsearch';

deebloo avatar Oct 04 '16 14:10 deebloo

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.

ldt avatar Oct 05 '16 08:10 ldt

I was successful to add JQuery library, you can try:

  1. Run npm install jquery --save and npm install @types/jquery --save-dev
  2. Add types: ["jquery"] in file src/tsconfig.json
  3. Add declaration declare var $: JQueryStatic in file src/typings.d.ts
  4. Add import * as $ from 'jquery' from where you want to use

t2d-ev avatar Oct 06 '16 04:10 t2d-ev

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.

ldt avatar Oct 06 '16 08:10 ldt

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": [
      ],

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.

ldt avatar Oct 06 '16 09:10 ldt

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?

ravishan110 avatar Mar 13 '19 10:03 ravishan110

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.