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

@clerk/clerk-js support #268

Closed
fagiani opened this issue Apr 7, 2023 · 5 comments
Closed

@clerk/clerk-js support #268

fagiani opened this issue Apr 7, 2023 · 5 comments

Comments

@Bubblyworld
Copy link

Hey, sorry for the delay - so it seems like this is actually an interop issue with CommonJS and ESM. @clerk/clerk-js is a CJS module, so if you import it from an ESM context (which is what you're doing in the sandbox) then the thing node.js does is to treat the CJS module as if it's wrapped in an ESM module that does this: export default module.exports;.

So import Clerk from "@clerk/clerk-js" means that Clerk is set to the whole module.exports object:

{ ClerkAPIResponseError: [Getter],
  MagicLinkError: [Getter],
  MagicLinkErrorCode: [Getter],
  default: [Getter],
  isClerkAPIResponseError: [Getter],
  isKnownError: [Getter],
  isMagicLinkError: [Getter],
  isMetamaskError: [Getter],
  parseError: [Getter],
  parseErrors: [Getter] }

That default key is actually the class that you're after. JSPM follows the same rules as node.js when transpiling to ESM, so that's why it's happening here as well. You can fix it by just using the default key:

import Clerk from "@clerk/clerk-js";
  const clerkFrontendApi = 'clerk.[your-domain].com';
  const clerk = new Clerk.default(clerkFrontendApi);
  await clerk.load({
    // Set load options here...
  });

The reason the @clerk/clerk-js docs don't do this is because some bundlers don't follow the node.js interop rules and will set the CJS module's default export to module.exports.default if it exists. This inconsistency is quite a pain point, you can see some old discussions about it for rollup, for instance:
rollup/plugins#635

@Bubblyworld
Copy link

So I guess that's a no-fix from our side, since we stick quite closely to node.js semantics - ideally @clerk/clerk-js should export an ESM build alongside their CJS stuff with the correct exports (and then all bundlers/runtimes would behave the same way here).

@fagiani
Copy link
Author

fagiani commented Apr 19, 2023

@Bubblyworld thanks a bunch for taking the time to investigate that. Being this a broader issue that could potentially affect other modules, would you agree with a README update that could have a short copy that can save some people's time? If so, I'd be willing to contribute.

@fagiani
Copy link
Author

fagiani commented Apr 19, 2023

Something else I'd like to understand. After initializing the module, one of the possible usages of it is calling a function like:

Clerk.openSignIn();

no matter if i add .default in the middle, it will return:

TypeError: Clerk.default.openSignIn is not a function at sandbox:43:19

I've also tried to use the instantiated clerk on the main example without any luck.

Would you have any clues on how that would be approached?

Thanks again!

@guybedford
Copy link
Member

@fagiani that would be more of an API issue to follow-up on, if you try importing the library in Node.js directly it would give the exact same semantics.

Closing as I believe this is resolved, but just let us know if we can help further.

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