beyonk-group / gdpr-cookie-consent-banner

A GDPR compliant cookie consent banner implementation
MIT License
251 stars 42 forks source link

Cannot use this in Svelte with Type: Module when running in development mode #47

Closed NathanNobert closed 1 year ago

NathanNobert commented 1 year ago

I receive an error when trying to use this in a project when running locally using Svelte. The error is:

 The requested module '/node_modules/.vite/deps/@beyonk_gdpr-cookie-consent-banner.js?v=6c7b1300' does not provide an export named 'default'

I believe this is due to my Svelte project using Modules and typescript, as in my package.json file I have:

"type": "module"

I do not receive this error when building and deploying to production however.

0gust1 commented 1 year ago

@NathanNobert,

(edit) if you use vite in your svelte project

Try adding

  optimizeDeps: {
    exclude: ['@beyonk/gdpr-cookie-consent-banner']
  }

in your vite.config.js or vite.config.ts file

It seems that it's sometimes still not obvious how to consume svelte component which are external dependencies. Vite, seeing that the dep is in the node_modules directory tries to handle the import as a "non-svelte-potentially-cjs module".

0gust1 commented 1 year ago

Small note for Sveltekit users:
the vite trick above seems not necessary anymore with the ^10.0.0 version of this package!