Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
schalkneethling opened this issue Jan 27, 2024 · 1 comment
Closed

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

schalkneethling opened this issue Jan 27, 2024 · 1 comment

Comments

@schalkneethling
Copy link

schalkneethling commented Jan 27, 2024

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
Copy link
Author

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";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant