knocklabs / javascript

Official JavaScript packages for interacting with Knock
https://knock.app/
MIT License
16 stars 3 forks source link

Doesn't work with Svelte (so most likely doesn't work with Vite in general?) #20

Closed rainx0r closed 8 months ago

rainx0r commented 1 year ago

Hi!

I'm working on a hobby project written in SvelteKit hosted on Vercel, and I'm looking for a notification solution. After seeing Vercel's blogpost giving Knock a glowing recommendation I decided to try and integrate it into my app, however the client just does not seem to initialise. Might be a problem when running under Vite (the bundler Svelte uses)?

This is the code I have in a utility file:

import { PUBLIC_KNOCK_API_KEY } from '$env/static/public';
import Knock from '@knocklabs/client';

const client = new Knock(PUBLIC_KNOCK_API_KEY);

export default client;

And even just this fails to run with the following stack trace:

TypeError: Cannot read properties of undefined (reading 'default')
    at MY_PROJECT_SRC/knock/client.ts:4:19
    at async instantiateModule (file:///MY_PROJECT/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:52224:9)
TypeError: Cannot read properties of undefined (reading 'default')
    at MY_PROJECT_SRC/knock/client.ts:4:19
    at async instantiateModule (file:///MY_PROJECT/node_modules/vite/dist/node/chunks/dep-5e7f419b.js:52224:9)

Obviously I stubbed out my actual file paths in the above error snippet for privacy reasons.

I was also getting a warning about needing "type": "module" in the package.json file for the Knock client, which I put in myself locally to see if it made any difference and it made the warning go away but it did not fix the problem, it still errors out and doesn't work.

Would be great if you could look into this! Though seeing how the client documentation is somewhat out of date with the actual API and a few months old stale issues in this repo I assume this client is not at the top of your priorities... Pity, as it looks like a great solution for my use case!

luisdoniad commented 1 year ago

@evangelos-ch Hello, were you finally able to get the client running on Vite? We are evaluating potential notification services, and our frontend project is based on Vue/Vite. Thanks

rainx0r commented 1 year ago

@evangelos-ch Hello, were you finally able to get the client running on Vite? We are evaluating potential notification services, and our frontend project is based on Vue/Vite. Thanks

Hi Luis! Nope, I never really ended up getting it working, I deferred the implementation of the notification system hoping I would get a response here or something in the meantime. Then a few months later I shelved my project for various reasons so I ended up not fiddling with it in the end.

The commit history doesn't seem to suggest that this is fixed yet though, so I wouldn't bet on it working now, but who knows.

kylemcd commented 8 months ago

This should be fixed by #22

patrikengborg commented 7 months ago

I still have this problem in a SvelteKit app. It seems this fix: https://github.com/knocklabs/javascript/commit/59ce04400058e7d08925a20f925e80c1760e23e0 has been merged to @knocklabs/react but not to @knocklabs/client.

Can you please merge it to @knocklabs/client and see if that resolves it?

kylemcd commented 7 months ago

@patrikengborg Working through a fix now, will update you here when it's released.

kylemcd commented 7 months ago

@patrikengborg v0.1.9 is now available and should fix the issues that you are seeing: https://www.npmjs.com/package/@knocklabs/react/v/0.1.9

patrikengborg commented 7 months ago

It works! Great job, thanks a lot!