【问题标题】:ERROR in ./node_modules/@elastic/elasticsearch/lib/Connection.js Module not found: Error: Can't resolve 'http'./node_modules/@elastic/elasticsearch/lib/Connection.js 中的错误找不到模块:错误:无法解析“http”
【发布时间】:2019-10-16 09:59:37
【问题描述】:

我正在做一个 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);
  }

}

错误:

./node_modules/@elastic/elasticsearch/lib/Connection.js 中的错误 找不到模块:错误:无法解析“http” 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

./node_modules/@elastic/elasticsearch/lib/Connection.js 中的错误 找不到模块:错误:无法解析“https” 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

./node_modules/@elastic/elasticsearch/lib/Transport.js 模块中的错误 未找到:错误:无法解析 'os' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

./node_modules/decompress-response/index.js 中的错误模块未找到: 错误:无法解析中的“流” 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules\decompress-response'

./node_modules/@elastic/elasticsearch/lib/Transport.js 模块中的错误 未找到:错误:无法解析“zlib” 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

./node_modules/decompress-response/index.js 中的错误模块未找到: 错误:无法解析“zlib” 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules\decompress-response'

【问题讨论】:

    标签: angular typescript elasticsearch angular6


    【解决方案1】:

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

    不再支持浏览器,因为它将通过另一个模块 @elastic/elasticsearch-browser 分发。此模块仅适用于 Node.js。

    【讨论】:

      【解决方案2】:

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

      import { Client } from 'elasticsearch-browser';
      

      【讨论】:

      • elasticsearch-browser是elasticsearch第6版API,这个新版本改变了你导入angular import {Client} from '@ elastic / elasticsearch'; 的方式
      猜你喜欢
      • 2019-06-13
      • 2021-01-30
      • 1970-01-01
      • 2021-02-20
      • 2021-02-21
      • 2022-08-07
      • 2017-10-08
      • 2021-01-12
      • 1970-01-01
      相关资源
      最近更新 更多