magiclabs / magic-admin-js

Magic admin Node.js SDK makes it easy to leverage Decentralized ID tokens to protect routes and restricted resources for your application.
https://docs.magic.link/admin-sdk/node-js
MIT License
75 stars 33 forks source link

ESM Imports are Broken in 1.4.0 #85

Closed olokobayusuf closed 8 months ago

olokobayusuf commented 2 years ago

✅ Prerequisites

🐛 Description

The title's pretty self-explanatory:

🧩 Steps to Reproduce

  1. mkdir repro && cd repro
  2. npm init -y
  3. npm install @magic-sdk/admin
  4. echo "import { Magic } from '@magic-sdk/admin'" > index.js
  5. Add "type": "module" to package.json
  6. node index.js.

🤔 Expected behavior

There shouldn't be an import error.

😮 Actual behavior

You'll get the following error:

import { Magic } from "@magic-sdk/admin"
         ^^^^^
SyntaxError: Named export 'Magic' not found. The requested module '@magic-sdk/admin' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@magic-sdk/admin';
const { Magic } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Fix

Remove the following from Magic SDK's package.json:

"exports": {
  ".": {
    "import": "./dist/esm/index.js",
    "require": "./dist/cjs/index.js"
  }
},

💻 Code Sample

See above.

🌎 Environment

Software Version(s)
@magic-sdk/admin 1.4.0
Node 17.8.0
npm 8.5.5
Operating System macOS 12.2.1
98mux commented 2 years ago

i fixed this in the npm package magic-admin-esm by writing "type":"module" in the package.json and making sure that things were linked properly with the .js extension (also by linking to .index instead of directories).

Like this export { MagicAdminSDK as Magic } from './core/sdk'; -> export { MagicAdminSDK as Magic } from './core/sdk.js';

https://github.com/98mux/magic-admin-js

skoshx commented 2 years ago

I have no clue how this still hasn't been resolved, this needs to be fixed ASAP, this just bricked the log in functionality of my production site…

Thanks a lot @98mux for your solution, saved my ass :D

skoshx commented 1 year ago

friendly bump, didn't this company raise $27 mil recently, where is it going? 😅

@FYJen

98mux commented 1 year ago

friendly bump, didn't this company raise $27 mil recently, where is it going? sweat_smile

@FYJen

Lmao yeah, I've recently considering moving to MojoAuth instead (havent used it yet so can't say it is better, but atleast it is way cheaper)

ayv8er commented 1 year ago

Thank you for bringing this to our attention, we'll get this resolved.

ayv8er commented 1 year ago

The priority level of this issue has been mitigated with several workarounds such as the one proposed above, using an earlier version or requiring the file. We will keep this on our radar and provide a resolution.

skoshx commented 1 year ago

@ayv8er Wait a second, so instead of Magic Labs fixing this, I'm gonna have to rely on a fork made by @98mux because you don't have the time to change a couple of imports to fix a breaking problem for modern frameworks (eg. SvelteKit)?

Putting the burden of fixing bugs & maintaining fixed versions of YOUR PACKAGES on the open-source community, even after raising $27 million? That's crazy :D

98mux commented 1 year ago

@skoshx hahaha yeah this is actually wild, they cant bother to fix a game breaking bug that takes 3 minutes to fix lmao. Ive switched to mojoauth, was a relative easy switch, atleast when i dont need the web3 stuff. It would probably take them less time to fix this than the time they have probably spent discussing this issue

skoshx commented 1 year ago

@98mux Yeah, seems like they're working more at the speed of an antiquated software company like SAP, rather than the speed of a startup :/ Good that you found a better alternative though, MojoAuth definitely looks good! Unfortunately, I need the web3 stuff, and MojoAuth hasn't released it yet…

olivercoad commented 1 year ago

@ayv8er Please re-open. Even if it's low priority because there are workarounds, it's should still be an open issue to make it easier to find and to make it clear that it hasn't been fixed in 1.4.1.

Also @98mux 's package doesn't seem to work for me (using svelte-kit with adapter-node). It has a runtime error: can't dynamically require 'crypto'. The fork doesn't have issues enabled and I doubt magiclabs would want issues raised here for someone's fork, so there's not really a good place to discuss it...

pkayfire commented 1 year ago

@ayv8er you guys ever going to fix this? We're currently using a fork of the magic-admin-js because of this issue.

FireMakeThunder commented 1 year ago

This was a problem for me and I did what OP did to package.json in @magic-sdk/admin .

I am using Sveltekit and it appears to be working fine in development.

CryptoGraffe commented 8 months ago

This is a breaking issue for us at Coordinape. The fork above is super out of date. Please re-open and advise.

am-hernandez commented 8 months ago

Hi @olokobayusuf , cc @98mux @skoshx @olivercoad @pkayfire @FireMakeThunder @CryptoGraffe

We have released an update to the Admin SDK that resolves this issue. The fix is in version 2.1.0.

Please give it a try at your earliest convenience! 🙏

olokobayusuf commented 8 months ago

@am-hernandez we're moving over to Clerk.

paro-paro commented 1 week ago

I believe this is still an issue in the latest version of the package: 2.4.1. Locking down the version to 2.1.0 does work.

reproduction