angular - ./node_modules/@elastic/elasticsearch/lib/Connection.js 中出现错误 找不到模块 : Error: Can't resolve 'http'

标签 angular typescript elasticsearch angular6

我正在开发一个 Angular 6 项目,我将服务器上的 Elasticsearch 版本从 6 更新到 7,在更改旧 API 的代码以使用新 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文档(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.

关于angular - ./node_modules/@elastic/elasticsearch/lib/Connection.js 中出现错误 找不到模块 : Error: Can't resolve 'http' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56383445/

相关文章:

javascript - 编写递归搜索函数以返回对象;

javascript - 避免两个相同的组件互相改变 Angular

angular - 装饰器不支持对非导出类的引用

Elasticsearch - 映射冲突错误从 1.5 升级到 2.0

json - 使用 pg-promise 格式化 JSON 输出,将外键封装为对象

angular - 如何对 Angular @Output 进行单元测试

typescript - 在 Typescript 中使用 forEach 时如何分配类型(未知类型的对象)

date - Elasticsearch中日期数据类型的平均聚合?

elasticsearch - elasticsearch批量转储数十万个文档

angular - 将 Angular CLI 构建日志保存到文件