1

Trying to implement Magic in NextJS using TypeScript.

Following this example/tutorial, which uses JS not TS: https://github.com/magiclabs/example-nextjs

Problem: when importing Magic like so

import { Magic } from "magic-sdk";

function createMagic() {
  if (typeof window === "undefined") return null;
  return new Magic(process.env.NEXT_PUBLIC_MAGIC_PUBLISHABLE_KEY!);
}

export const magic = createMagic();

the following error occurs:

SyntaxError: Cannot use import statement outside a module

2

1 Answer 1

2

This is because Magic uses ESM style modules.

Solved using:

yarn add next-transpile-modules

and modifying my next.config.js to:

const withTM = require("next-transpile-modules")([
  "magic-sdk",
  "@magic-sdk/provider",
  "@magic-sdk/types",
  "@magic-sdk/commons",
]);

module.exports = withTM({
  reactStrictMode: true,
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.