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

Error: global is not defined - when creating a matrix client #4450

Open
ChrisKlp opened this issue Oct 15, 2024 · 1 comment
Open

Error: global is not defined - when creating a matrix client #4450

ChrisKlp opened this issue Oct 15, 2024 · 1 comment

Comments

@ChrisKlp
Copy link

Matrix SDK is not compatible with the latest version of Next.js (14.2.15). Creating a client causes an error in the amendClientOpts function, where it uses the global variable, which is not defined in this environment. The error started occurring with matrix-js-sdk version 34.4.0. The error appears on this line, but the global variable is used multiple times.

The code is simple. Here’s an example.

"use client";

import { createClient, MatrixClient } from "matrix-js-sdk";
import React, { useEffect, useRef } from "react";

export default function HomePage() {
  const matrixClient = useRef<MatrixClient | null>(null);

  useEffect(() => {
    matrixClient.current = createClient({ baseUrl: "https://matrix.org" });
  }, []);

  return <div>page</div>;
}

Here, I have a screenshot of the error.

image

The browser recognizes globalThis but not global. global is available only in a node.js environment. How can I run Matrix on Next.js? Thanks in advance for the help.

@richvdh
Copy link
Member

richvdh commented Oct 15, 2024

I think matrix-js-sdk currently expects the environment to provide a polyfill for global. For example, Weback provides one.

I am very surprised that you found this started with 34.4.0, because as far as I know this code has not changed in years.

As for solutions: pull-requests to replace global with globalThis would be welcome.

In the meantime, maybe you can work around it. I'm not familiar with next.js but maybe you can configure it to provide a polyfill? Alternatively, if you provide a concrete implementation of IStore to createClient, you'll avoid the problematic line:

let store = new MemoryStore({ localStorage });
let client = createClient({ store });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants