CacheControl / json-rules-engine

A rules engine expressed in JSON
ISC License
2.54k stars 455 forks source link

Uncaught TypeError: (0 , _hashIt2.default) is not a function #344

Open dcowan-e-courier opened 11 months ago

dcowan-e-courier commented 11 months ago

When calling run getting this error. This is implementing the simple first example provided in the documentation in a react web app

fact.js:119 Uncaught TypeError: (0 , _hashIt2.default) is not a function
    at Function.hashFromObject (fact.js:119:1)
    at Fact.getCacheKey (fact.js:112:1)
    at Almanac._setFactValue (almanac.js:149:1)
    at Almanac._addConstantFact (almanac.js:136:1)
    at new Almanac (almanac.js:63:1)
    at Engine.run (engine.js:377:1)
chris-pardy commented 11 months ago

@dcowan-e-courier can you check which version of hash-it you have installed?

siawyoung commented 11 months ago

I'm seeing this error too. If it helps, I have the same code running successfully in a Node environment, but running it in a browser environment (Chrome) produces the above error. The exact version of hash-it on both environments:

hash-it@^6.0.0:
  version "6.0.0"
  resolved "https://registry.yarnpkg.com/hash-it/-/hash-it-6.0.0.tgz#188df5a8ca2f8e036690e35f2ef88bd9417ff334"
  integrity sha512-KHzmSFx1KwyMPw0kXeeUD752q/Kfbzhy6dAZrjXV9kAIXGqzGvv8vhkUqj+2MGZldTo0IBpw6v7iWE7uxsvH0w==
siawyoung commented 11 months ago

@dcowan-e-courier if it helps, I tried running this in a fresh NextJS environment (using create-next-app) instead of create-react-app. That works. My guess is it has something to do with the Webpack bundling in create-react-app and how it messes up default exports. I don't have time to explore this further but hopefully it unblocks you!

chris-pardy commented 11 months ago

@siawyoung @dcowan-e-courier this does seem to be related to the export mapping on the hash-it package and how something like webpack is resolving that.

MohHamoud commented 11 months ago

I am having the same error in an existing project, I reproduced the issue by creating a project using create-react-app as @siawyoung mentioned, is there a fast way to fix it? I understand that create-next-app creates a next.js application, which I don't prefer doing.

chris-pardy commented 11 months ago

@MohHamoud I haven't reproduced this issue since it's more related to webpack than json-rules-engine but...

The issue is likely that the webpack version that create-react-app is using doesn't understand the export mapping in hash-it's package.json.

Option 1: Downgrade hash-it - depending on your package manager of choice you may be able to force the resolution of hash-it to an older version that doesn't have the export mapping. Option 2: Upgrade webpack. I'm not sure which version of webpack create-react-app uses but the export mapping is a fairly new package.json feature so we older webpack versions may not support it. This will likely requiring ejecting.

Overall I'd probably go the Option 2 route. specifically because create-react-app is no longer supported by the react team. If you don't wish to go with one of the various react-ecosystem setups then you're best bet is likely a roll-your-own approach.

MohHamoud commented 10 months ago

@chris-pardy I cloned the project, then copied the src files to my project and "yarn add"-ed the required dependencies (including hash-it) it worked fine, without specifying the version of the hash-it nor the webpack

chris-pardy commented 10 months ago

@MohHamoud this makes sense since we transform the source from modules to common / require. As part of that babel is doing a default import transformation. Then the webpack resolution is giving us the wrong version of the hash-it library.