ERROR in ./node_modules/@elastic/elasticsearch/lib/Connection.js Module not found: Error: Can't resolve 'http'

ERROR in ./node_modules/@elastic/elasticsearch/lib/Connection.js Module not found: Error: Can't resolve 'http'

我正在做一个 angular 6 项目,我将服务器上的 elasticsearch 版本从 6 更新到 7,在更改旧 API 的代码以使用新代码后,发生了这些错误,有人知道如何纠正吗?

angular 应用程序是在 visual studio 代码上创建的,elasticsearch 在 linux 服务器上。

import { Injectable } from '@angular/core';
// import { Clientex } from 'elasticsearch-browser';
import { Client } from '@elastic/elasticsearch';
// import * as elasticsearch from 'elasticsearch-browser';
// import { concatMapTo } from 'rxjs/operators';
// import { stringify } from '@angular/core/src/render3/util';


@Injectable({
  providedIn: 'root'
})

export class ElasticsearchService {

  private client: Client;
  // private clientex: Clientex;

  constructor() {
    if (!this.client) {
      this.connect();
    }
  }

  private connect() {
    /*this.client = new Client(
      {node: 'http://localhost:9200'}
    );*/
    this.client = new Client();
  }

  isAvailable(): Promise<any> {
    return this.client.ping({
    });
  }


  // tslint:disable-next-line:member-ordering
  private queryalldocs = {
    'query': {
      'match_all': {}
    }
  };

  getAllDocuments(_index, _type): any {
    this.client.search({
      index: _index,
      body: this.queryalldocs
    }, {
      ignore: [404]
    }, (err, { body, statusCode, headers, warnings }) => {
      if (err) { console.log(err); }
    });
}

  getAllDocumentsByKey(_index, _type, campo, valor) {
    const string = 'let match = {' + campo + ':' + valor + '}';
    return this.client.search({
      index: _index,
      type: _type,
      body: {
           // tslint:disable-next-line:no-shadowed-variable
           eval(string) {}
      }
    });
  }

  createDocument(value: any) {
    // return this.client.index(value);
    this.client.create(value);
  }

}

错误:

ERROR in ./node_modules/@elastic/elasticsearch/lib/Connection.js Module not found: Error: Can't resolve 'http' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

ERROR in ./node_modules/@elastic/elasticsearch/lib/Connection.js Module not found: Error: Can't resolve 'https' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

ERROR in ./node_modules/@elastic/elasticsearch/lib/Transport.js Module not found: Error: Can't resolve 'os' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

ERROR in ./node_modules/decompress-response/index.js Module not found: Error: Can't resolve 'stream' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules\decompress-response'

ERROR in ./node_modules/@elastic/elasticsearch/lib/Transport.js Module not found: Error: Can't resolve 'zlib' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

ERROR in ./node_modules/decompress-response/index.js Module not found: Error: Can't resolve 'zlib' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules\decompress-response'

我相信您需要安装 elasticsearch-browser 然后:

import { Client } from 'elasticsearch-browser';

根据 elasticsearch 文档 (https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/5.x/breaking-changes.html),所有浏览器客户端都是通过 elasticsearch-browser npm 包处理的:

No more browser support, for that will be distributed via another module, @elastic/elasticsearch-browser. This module is intended for Node.js only.