nextapps-de / flexsearch

Next-Generation full text search library for Browser and Node.js
Apache License 2.0
12.54k stars 492 forks source link

How to use the library in Angular ? #203

Closed Rebolon closed 3 years ago

Rebolon commented 3 years ago

Hi,

i'm trying to import this library into Angular (v11). I've read the docs and also this issue https://github.com/nextapps-de/flexsearch/issues/196 without any success.

Angular 11 is delivered with:

Here is my tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

In fact in my IDE, the "require" is always underlined in red: TS2591 cannot find name 'require'... And when the application run, it fails because flexsearch is just an empty object and so i cannot call create function. Any clue ? I've tried to add this property in tsconfig.json but still a failure : "esModuleInterop": true

const FlexSearch = require('flexsearch');

@Injectable()
export class Cites {
  // didn't find a way to specify type Index ?
  protected index;
  public constructor() {
    const index = FlexSearch.create({
      // mandatory:
      encode: (str) => str,
      // optionally:
      rtl: false,
      stemmer: {},
      // matcher: {},
      filter: false,
      tokenize: 'strict',
      depth: 3,
      doc: {
        id: 'id',
        field: ['cite', 'author']
      }
    });
  }

Each time i get this error:

ERROR Error: Uncaught (in promise): TypeError: flexsearch__WEBPACK_IMPORTED_MODULE_5___default.a.create is not a function
TypeError: flexsearch__WEBPACK_IMPORTED_MODULE_5___default.a.create is not a function

I've also tryed another way: import FlexSearch from 'flexsearch'; This time the FlexSearch is not undefined, but it's an empty object...

Thanks for help,

ts-thomas commented 3 years ago

Please change over to the version >= 0.7.x, thanks a lot.