typesense-instantsearch-adapter icon indicating copy to clipboard operation
typesense-instantsearch-adapter copied to clipboard

typesense-instantsearch-adapter is unfriendly for pure Javascript/ReScript projects

Open sprkv5 opened this issue 2 years ago • 3 comments

Description

npm install typesense-instantsearch-adapter provides a dependency that has index.js missing. This should be fine for TypeScript projects. For pure JavaScript/ReScript projects, attempting to build the example results in the following error:

var adapter = new TypesenseInstantsearchAdapter(config); ^

TypeError: TypesenseInstantsearchAdapter is not a constructor

Steps to reproduce

  1. mkdir typesense-demo && cd typesense-demo
  2. npm install typesense-instantsearch-adapter
  3. Copy and paste the JavaScript example from : https://typesense.org/docs/guide/search-ui-components.html#walk-through
    // index.mjs -- since we're using ESM
    import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter";

    const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
    ...
  1. node index.mjs
    file:///C:/Users/User/Projects/typesense-demo/index.mjs:3
    const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
                                          ^

    TypeError: TypesenseInstantSearchAdapter is not a constructor
    ...

Expected Behavior

index.mjs file should run without error

Actual Behavior

I find the following error:

    file:///C:/Users/User/Projects/typesense-demo/index.mjs:3
    const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
                                                                    ^

    TypeError: TypesenseInstantSearchAdapter is not a constructor
    ...

because index.js is missing in the dependency here: node_modules/typesense-instantsearch-adapter/index.js

Metadata

Typsense Version: 0.22.2

OS: Windows

sprkv5 avatar Apr 08 '22 16:04 sprkv5

+1, did you ever manage to solve this?

For now, I managed to get it to work with import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter/src/TypesenseInstantsearchAdapter"; instead of import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter";

jhotujec avatar May 21 '23 08:05 jhotujec

@jhotujec That solution doesn't work for me. I get this error now instead:

2023-06-05 13:55:52 (node:139) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
2023-06-05 13:55:52 (Use `node --trace-warnings ...` to show where the warning was created)
2023-06-05 13:55:52 /app/node_modules/typesense-instantsearch-adapter/src/TypesenseInstantsearchAdapter.js:3
2023-06-05 13:55:52 import { Configuration } from "./Configuration";
2023-06-05 13:55:52 ^^^^^^
2023-06-05 13:55:52 
2023-06-05 13:55:52 SyntaxError: Cannot use import statement outside a module

eminos avatar Jun 05 '23 11:06 eminos

@eminos not sure, I used it like this and it works

import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter/src/TypesenseInstantsearchAdapter";

const searchAdapter = function (searchParams = {
  query_by: "title",
}) {
  const qtypesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
    server: {
      apiKey: "<your-api-key>",
      nodes: [
        {
          host: "<your-host>",
          path: "",
          port: "443",
          protocol: "https",
        },
      ],
      cacheSearchResultsForSeconds: 0,
    },
    additionalSearchParameters: searchParams
  });
  return qtypesenseInstantsearchAdapter.searchClient;
};

export default searchAdapter;

jhotujec avatar Jun 05 '23 13:06 jhotujec