nextapps-de / flexsearch

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

new Document causes `.default is not a constructor` error #426

Closed schalkneethling closed 9 months ago

schalkneethling commented 9 months ago

Hey All,

I have been using Flexsearch for a pretty long time now and it worked amazingly well. That is why I was so surprised when all my tests started to fail with the update to 0.7.43. I pulled down the branch and tried to run it locally. The first thing I noticed was that the build failed because I was doing the following:

import { Document } from "flexsearch";

I changed this to use a default exports as I can see in the source code that it is indeed exported as the default in document.js

import Document from "flexsearch";

However, with that, the following code triggers an error in the developer console:

React.useEffect(() => {
  const jsonURL = process.env.REACT_APP_TESTING
    ? "/tools-test.json"
    : "/tools.json";

  async function fetchData() {
    const response = await fetch(jsonURL);
    const tools = await response.json();

    setTools(tools);

    const index = new Document({ // <<-- THE ERROR IS THROWN HERE
      document: {
        index: ["tag", { field: "title", tokenize: "forward" }],
      },
    });

    tools.forEach((tool) => {
      index.add(tool);
    });

    setToolsIndex(index);
  }

  fetchData();
}, []);

Error

TypeError: flexsearch__WEBPACK_IMPORTED_MODULE_1__.default is not a constructor

Any idea why this is happening? I can see that the code in document.js uses new Document itself so curious as to why Webpack is mad. Thanks!

Update: I converted the project to Vite and I am still seeing the same error:

TypeError: Document is not a constructor
    at fetchData (App.jsx:53:21)
schalkneethling commented 9 months ago

Resolved with a combination of the following:

i.e. in ESM import as shown above, but for quick reference in the Document case:

import Document from "flexsearch/dist/module/document";