magic-admin-js icon indicating copy to clipboard operation
magic-admin-js copied to clipboard

ESM Imports are Broken in 1.4.0

Open olokobayusuf opened this issue 3 years ago โ€ข 5 comments

โœ… Prerequisites

  • [x] Did you perform a cursory search of open issues? Is this bug already reported elsewhere?
  • [x] Are you running the latest SDK version?
  • [x] Are you reporting to the correct repository (@magic-sdk/admin)?

๐Ÿ› 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

olokobayusuf avatar Apr 02 '22 15:04 olokobayusuf

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

98mux avatar May 24 '22 17:05 98mux

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 avatar Aug 10 '22 01:08 skoshx

friendly bump, didn't this company raise $27 mil recently, where is it going? ๐Ÿ˜…

@FYJen

skoshx avatar Oct 10 '22 21:10 skoshx

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)

98mux avatar Oct 11 '22 07:10 98mux

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

ayv8er avatar Oct 12 '22 15:10 ayv8er

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.

ayv8er avatar Nov 04 '22 21:11 ayv8er

@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

skoshx avatar Nov 04 '22 22:11 skoshx

@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

98mux avatar Nov 04 '22 23:11 98mux

@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โ€ฆ

skoshx avatar Nov 05 '22 00:11 skoshx

@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...

olivercoad avatar Nov 16 '22 11:11 olivercoad

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

pkayfire avatar Jan 11 '23 04:01 pkayfire

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.

FireMakeThunder avatar Apr 06 '23 03:04 FireMakeThunder

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

CryptoGraffe avatar Jan 16 '24 04:01 CryptoGraffe

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! ๐Ÿ™

am-hernandez avatar Jan 19 '24 20:01 am-hernandez

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

olokobayusuf avatar Jan 22 '24 00:01 olokobayusuf