matrix-org / matrix-js-sdk

Matrix Client-Server SDK for JavaScript
Apache License 2.0
1.49k stars 577 forks source link

global is not defined (Nuxt) #3971

Closed muffincode closed 2 days ago

muffincode commented 6 months ago

Hi, I'm using matrix-js-sdk@30.3.0 in a Nuxt app and encounter this error when loading the app

Uncaught ReferenceError: global is not defined (NuxtJS 15
[matrix-js-sdk.js:180169:9](http://localhost:3000/_nuxt/node_modules/.cache/vite/client/deps/matrix-js-sdk.js?v=52b26584))

I can fix this by changing the Vite config as such:

// in nuxt.config.js
  vite: {
    define: {
      global: 'window'
    },
  },

Then, serving the project locally works fine, but when building, it seems to replace all instances of "global" in the filenames etc, breaking the build.

For example, the component stored in components/global/buttons/button-text.vue

 ERROR  [vite:vue] Could not load /home/xxx/yyy/components/window/button/button-text.vue?vue&type=style&index=0&scoped=5bbaaa36&lang.css (imported by components/global/button/button-text.vue): ENOENT: no such file or directory, open '/home/xxx/yyy/components/window/button/button-text.vue'

is there any other way to give the sdk what it wants without changing the vite config?

richvdh commented 2 days ago

Did you get anywhere with this?

I'm not familiar with Vite, but it seems like there ought to be a way to configure it to provide a global object, without it renaming files.

muffincode commented 2 days ago

I managed by specifying the environment

export default defineNuxtConfig({
  $development: {
    vite: {
      define: {
        global: 'window'
      },
    },
  },