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

indexer is not a function #28

Open
antiantivirus opened this issue Aug 17, 2022 · 4 comments
Open

indexer is not a function #28

antiantivirus opened this issue Aug 17, 2022 · 4 comments

Comments

@antiantivirus
Copy link

antiantivirus commented Aug 17, 2022

getting the error "indexer is not a function" with the following code. Written in javascript and deployed on netlify serverless function

const sanityClient = require('@sanity/client')
const algoliasearch = require('algoliasearch')
const indexer = require('sanity-algolia')

const algolia = algoliasearch(...)
const sanity = sanityClient({...})

exports.handler = async (event, context) => {
if (event.headers['content-type'] !== 'application/json') {
return {
statusCode: 400,
body: "Bad request"
}
}

const algoliaIndex = algolia.initIndex('products')

const sanityAlgolia = indexer(
{
product: {
index: algoliaIndex,
projection: { _type, _rev, "objectID": _id, "slug":slug.current, _createdAt, title, "categories": categories[]->name, "image": mainImage.asset->url description, shortSummary, features, benefits, applications, "description": pt::text(description), }
},
},
)

sanityAlgolia
.webhookSync(sanity, event.body)
.then(() => {
return {
statusCode: 200,
body: "All is good"
}
}
)
}
@Mehoff
Copy link

Mehoff commented Dec 20, 2022

My solution was adding .default.
const indexer = require('sanity-algolia').default

@kamerat
Copy link

kamerat commented Jun 7, 2023

Might be related / fixed by this:
sveltejs/kit#6694

"module": "dist/sanity-algolia.esm.js",

https://publint.dev/[email protected] states:
pkg.module is used to output ESM, but pkg.exports is not defined. As NodeJS doesn't read pkg.module, the ESM output may be skipped. Consider adding pkg.exports to export the ESM output. pkg.module can usually be removed alongside too. (This will be a breaking change)

HAS_MODULE_BUT_NO_EXPORTS:
If the package has a module field, but has no exports field, suggest to use exports instead. This is because Node.js doesn't recognize the module field, so instead using exports would increase compatibility with it.

If the package isn't meant for Node.js usage, it is safe to ignore this suggestion, but it is still recommended to use exports whenever possible.

@kamerat
Copy link

kamerat commented Jun 7, 2023

As suggested from https://publint.dev/[email protected]
I modified my local node_modules\sanity-algolia\package.json to the following, and that made it work for me.

{
  "version": "1.1.0",
  "license": "MIT",
  "main": "dist/index.js",
  "typings": "dist/index.d.ts",
  "files": [
    "dist",
    "src"
  ],
  "engines": {
    "node": ">=10"
  },
  "scripts": {
    "start": "tsdx watch",
    "build": "tsdx build",
    "test": "tsdx test",
    "lint": "tsdx lint src test",
    "prepare": "tsdx build"
  },
  "husky": {
    "hooks": {
      "pre-commit": "tsdx lint src test"
    }
  },
  "name": "sanity-algolia",
  "author": "Rune Botten",
+ "type": "module",
  "module": "dist/sanity-algolia.esm.js",
+ "exports": {
+     "import": "./dist/sanity-algolia.esm.js",
+     "require": "./dist/sanity-algolia.cjs.js"
+ },
  "devDependencies": {
    "@types/jest": "^26.0.14",
    "@types/stopword": "^0.3.0",
    "husky": "^4.3.0",
    "prettier": "^2.1.2",
    "tsdx": "^0.14.0",
    "tslib": "^2.0.2",
    "typescript": "^4.0.3"
  },
  "dependencies": {
    "algoliasearch": "^4.5.1",
    "stopword": "^1.0.3"
  }
}

I don't know if this would break something else, but could you take a look at maybe implementing this @runeb? :)

@kamerat
Copy link

kamerat commented Sep 13, 2023

Any updates on this?

kshreve added a commit to GoCanadaDev/sanity-algolia that referenced this issue Jan 25, 2024
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

3 participants